
- Created a dedicated base_class.py file to define the SQLAlchemy Base - Updated all models to import Base from base_class.py instead of session.py - Modified migrations/env.py to properly import models and Base - Fixed circular dependency between models and base classes
23 lines
952 B
Python
23 lines
952 B
Python
from datetime import datetime
|
|
from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey, Boolean
|
|
from sqlalchemy.orm import relationship
|
|
|
|
from app.db.base_class import Base
|
|
from app.models.tag import post_tag
|
|
|
|
|
|
class Post(Base):
|
|
__tablename__ = "posts"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
title = Column(String(255), nullable=False)
|
|
content = Column(Text, nullable=False)
|
|
published = Column(Boolean, default=True)
|
|
author_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
|
|
created_at = Column(DateTime, default=datetime.utcnow)
|
|
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
|
|
|
# Relationships
|
|
author = relationship("User", back_populates="posts")
|
|
comments = relationship("Comment", back_populates="post", cascade="all, delete-orphan")
|
|
tags = relationship("Tag", secondary=post_tag, back_populates="posts") |