SHSH_Messenger
/
models.py
56 строк · 2.2 Кб
1import os
2from sqlalchemy import create_engine, Column, Integer, String, ForeignKey, DateTime, func
3from sqlalchemy.ext.declarative import declarative_base
4from sqlalchemy.orm import sessionmaker, relationship
5
6DATABASE_URL = os.getenv("DATABASE_URL", "postgresql://postgres:lolhilol@db/chatapp")
7
8engine = create_engine(DATABASE_URL)
9SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
10Base = declarative_base()
11
12class User(Base):
13__tablename__ = 'users'
14id = Column(Integer, primary_key=True, index=True)
15username = Column(String, unique=True, index=True)
16password = Column(String)
17last_seen = Column(DateTime, default=func.now())
18profile = relationship("UserProfileModel", uselist=False, back_populates="user")
19
20class UserProfileModel(Base):
21__tablename__ = 'profiles'
22id = Column(Integer, primary_key=True, index=True)
23user_id = Column(Integer, ForeignKey('users.id'))
24first_name = Column(String)
25last_name = Column(String)
26profile_photo = Column(String)
27user = relationship("User", back_populates="profile")
28
29class Chat(Base):
30__tablename__ = 'chats'
31id = Column(Integer, primary_key=True, index=True)
32user1_id = Column(Integer, ForeignKey('users.id'))
33user2_id = Column(Integer, ForeignKey('users.id'))
34user1 = relationship('User', foreign_keys=[user1_id])
35user2 = relationship('User', foreign_keys=[user2_id])
36
37class Message(Base):
38__tablename__ = 'messages'
39id = Column(Integer, primary_key=True, index=True)
40sender_id = Column(Integer, ForeignKey('users.id'))
41receiver_id = Column(Integer, ForeignKey('users.id'))
42chat_id = Column(Integer, ForeignKey('chats.id'))
43content = Column(String)
44timestamp = Column(DateTime, default=func.now())
45sender = relationship('User', foreign_keys=[sender_id])
46receiver = relationship('User', foreign_keys=[receiver_id])
47chat = relationship('Chat', foreign_keys=[chat_id])
48
49class UploadedImage(Base):
50__tablename__ = "uploaded_images"
51id = Column(Integer, primary_key=True, index=True)
52filename = Column(String, unique=True, index=True)
53file_path = Column(String, unique=True)
54
55
56Base.metadata.create_all(bind=engine)
57