class Category(db.Model): __tablename__ = "categories" id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String, nullable=False, unique=True) created_at = db.Column(db.DateTime, default=dt.datetime.utcnow) updated_at = db.Column(db.DateTime, default=dt.datetime.utcnow, onupdate=dt.datetime.utcnow) def __str__(self): return self.name def __repr__(self): return f"<Category id={self.id} name={self.name}>"
class User(db.Model): """ User Model for storing user related details """ __tablename__ = "users" id = db.Column(db.Integer, primary_key=True, autoincrement=True) email = db.Column(db.String(255), unique=True, nullable=False) username = db.Column(db.String(255), unique=True, nullable=False) password = db.Column(db.String(255), nullable=False) def __str__(self): return self.username def __repr__(self): return f"<User id={self.id} email={self.email} username={self.username}>"
class Tag(db.Model): __tablename__ = "tags" __table_args__ = {"extend_existing": True} id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String, nullable=False, unique=True) created_at = db.Column(db.DateTime, default=dt.datetime.utcnow) updated_at = db.Column(db.DateTime, default=dt.datetime.utcnow, onupdate=dt.datetime.utcnow) def __str__(self): return self.name def __repr__(self): return f"<Tag id={self.id} name={self.name}>"
class Article(db.Model): __tablename__ = "articles" id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String(110), nullable=False) slug = db.Column(db.String(200), unique=True, nullable=False) content = db.Column(db.Text, nullable=False) category_id = db.Column(db.Integer, db.ForeignKey("categories.id", ondelete="CASCADE"), nullable=True) created_at = db.Column(db.DateTime, default=dt.datetime.utcnow) updated_at = db.Column(db.DateTime, default=dt.datetime.utcnow, onupdate=dt.datetime.utcnow) category = db.relationship(Category, backref=db.backref("articles", lazy=True), lazy=False) tags = db.relationship( Tag, secondary=article_tags, lazy="subquery", backref=db.backref("articles", lazy=True), ) def __str__(self): return self.title def __repr__(self): return f"<Article id={self.id} title={self.title}>"
name = db.Column(db.String, nullable=False, unique=True) created_at = db.Column(db.DateTime, default=dt.datetime.utcnow) updated_at = db.Column(db.DateTime, default=dt.datetime.utcnow, onupdate=dt.datetime.utcnow) def __str__(self): return self.name def __repr__(self): return f"<Tag id={self.id} name={self.name}>" class TagSchema(ma.SQLAlchemyAutoSchema): model = Tag # include_fk = True хз что это tag_schema = TagSchema() tags_schema = TagSchema(many=True) article_tags = db.Table( "article_tags", db.Column("article_id", db.Integer, db.ForeignKey("articles.id"), primary_key=True), db.Column("tag_id", db.Integer, db.ForeignKey("tags.id"), primary_key=True), )