-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
66 lines (50 loc) · 2.44 KB
/
models.py
File metadata and controls
66 lines (50 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from datetime import datetime
from sqlalchemy import Column, Integer, String, Boolean, DateTime, ForeignKey, Index
from sqlalchemy.orm import relationship, Mapped, mapped_column
from sqlalchemy.sql.functions import func
if __package__ is None or __package__ == '':
from database import Base
else:
from .database import Base
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
fullname = Column(String(50), index=True)
email = Column(String(50), index=True, unique=True)
username = Column(String(50), unique=True, index=True)
is_active = Column(Boolean, default=True)
password_hash = Column(String)
created_at = Column(DateTime(timezone=True), server_default=func.now())
role = Column(String)
phone_number = Column(String(15), unique=True, index=True, nullable=True)
posts = relationship('Post', backref='owner', lazy='selectin') # backref, back_populates, lazy
comments = relationship('Comment', backref='commenter')
images = relationship('Image', backref='uploader', lazy='selectin')
class Post(Base):
__tablename__ = 'posts'
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
title = Column(String(50), index=True)
content = Column(String(1000))
created_at = Column(DateTime(timezone=True), server_default=func.now())
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
user_id = Column(Integer, ForeignKey('users.id'))
comments = relationship('Comment', backref='for_post', lazy='selectin')
class Comment(Base):
__tablename__ = 'comments'
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
content = Column(String(1000))
user_id = Column(ForeignKey('users.id'))
post_id = Column(ForeignKey('posts.id'))
created_at = Column(DateTime(timezone=True), server_default=func.now())
__table_args__ = (
Index('idx_user_post', 'user_id', 'post_id', unique=True),
)
class Image(Base):
__tablename__ = "images"
id: Mapped[int] = mapped_column(primary_key=True, index=True)
filename: Mapped[str] = mapped_column(String(255))
file_path: Mapped[str] = mapped_column(String(500))
content_type: Mapped[str] = mapped_column(String(100))
file_size: Mapped[int] = mapped_column(Integer)
uploaded_at: Mapped[datetime] = mapped_column(server_default=func.now())
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))