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
| from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Text, Boolean from sqlalchemy.orm import relationship from sqlalchemy.sql import func from database import Base
class User(Base): __tablename__ = "users" id = Column(Integer, primary_key=True, index=True) username = Column(String(50), unique=True, index=True, nullable=False) email = Column(String(100), unique=True, index=True, nullable=False) hashed_password = Column(String(255), nullable=False) is_active = Column(Boolean, default=True) created_at = Column(DateTime(timezone=True), server_default=func.now()) updated_at = Column(DateTime(timezone=True), onupdate=func.now()) posts = relationship("Post", back_populates="author", lazy="selectin") profile = relationship("UserProfile", back_populates="user", uselist=False)
class Post(Base): __tablename__ = "posts" id = Column(Integer, primary_key=True, index=True) title = Column(String(200), nullable=False, index=True) content = Column(Text, nullable=False) author_id = Column(Integer, ForeignKey("users.id"), nullable=False) is_published = Column(Boolean, default=False) created_at = Column(DateTime(timezone=True), server_default=func.now()) updated_at = Column(DateTime(timezone=True), onupdate=func.now()) author = relationship("User", back_populates="posts") tags = relationship("Tag", secondary="post_tags", back_populates="posts")
class UserProfile(Base): __tablename__ = "user_profiles" id = Column(Integer, primary_key=True, index=True) user_id = Column(Integer, ForeignKey("users.id"), unique=True, nullable=False) bio = Column(Text) avatar_url = Column(String(255)) user = relationship("User", back_populates="profile")
|