class CompanyTag(db.Model, TimestampMixin): __tablename__ = 'company_tags' __table_args__ = (db.UniqueConstraint('company_id', 'tag_id'), ) id = db.Column(db.Integer, primary_key=True) company_id = db.Column(db.Integer, db.ForeignKey('companies.id'), nullable=False) tag_id = db.Column(db.Integer, db.ForeignKey('tags.id'), nullable=False) tag = db.relationship(Tag, lazy='joined')
class Posts(db.Model): __tablename__ = 'posts' id = db.Column(db.Integer, primary_key=True) rid = db.Column(db.Integer, index=True, default=0) content = db.Column(db.Text) timestamp = db.Column(db.DateTime, default=datetime.utcnow) # 添加关联外键: '表名.字段' uid = db.Column(db.Integer, db.ForeignKey('users.id'))
class SearchCompany(db.Model, TimestampMixin): __tablename__ = 'search_companies' id = db.Column(db.Integer, primary_key=True) company_id = db.Column(db.Integer, db.ForeignKey('companies.id'), nullable=False) initial_index = db.Column(db.String(255), nullable=False) phoneme_index = db.Column(db.String(255), nullable=False)
class CompanyName(db.Model, TimestampMixin): __tablename__ = 'company_names' __table_args__ = (db.UniqueConstraint('company_id', 'lang'), ) id = db.Column(db.Integer, primary_key=True) company_id = db.Column(db.Integer, db.ForeignKey('companies.id'), nullable=False) name = db.Column(db.String(80), nullable=False) lang = db.Column(db.String(2), nullable=False)
class Hosts(db.Model): __tablename__ = 'hosts' id = db.Column(db.Integer, primary_key=True) url = db.Column(db.String, unique=False, nullable=True) muted = db.Column(db.Boolean, default=False, nullable=False) apikey_id = db.Column(db.Integer, db.ForeignKey('apikey.id'), nullable=True) stats = db.relationship('Stats', backref='hosts', lazy=True) def __init__(self, url, apikey_id): self.url = url self.apikey_id = apikey_id
class Stats(db.Model): __tablename__ = 'stats' id = db.Column(db.Integer, primary_key=True) code = db.Column(db.String, unique=False, nullable=True) response_time = db.Column(db.Integer, unique=False, nullable=True) time = db.Column(db.DateTime, index=False, unique=False, nullable=False) host_id = db.Column(db.Integer, db.ForeignKey('hosts.id'), nullable=True) def __init__(self, code, response_time, time, host_id): self.code = code self.response_time = response_time self.time = time self.host_id = host_id
class Author(BaseModel): """ Author database model. Used for storing author profile and personal details. """ __tablename__ = 'author' id = db.Column(db.Integer, primary_key=True, autoincrement=True) first_name = db.Column(db.String(255)) last_name = db.Column(db.String(255)) avatar = db.Column(db.String(255), nullable=True) books = db.relationship('Book', secondary=books, lazy='subquery', backref=db.backref('authors', lazy=True)) birthday = db.Column(db.Date, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) @property def name(self): """ Author full name """ first_name = f'{self.first_name} ' if self.first_name is not None else '' last_name = self.last_name if self.last_name is not None else '' return f'{first_name}{last_name}'.strip() def __repr__(self): """ Author representation """ return self.name
class Image(BaseModel, ModelMixin): """用户上传图片""" __tablename__ = 'image' user_id = db.Column(db.Integer, db.ForeignKey('user.id'), comment='上传的用户', default=None) default_show = db.Column(db.String(200), comment='默认上传显示的地址') raw = db.Column(db.Text)
""" Author related model """ from apps.extensions import db from .audit import BaseModel books = db.Table( 'author_book', db.Column('author_id', db.Integer, db.ForeignKey('author.id'), primary_key=True), db.Column('book_id', db.Integer, db.ForeignKey('book.id'), primary_key=True)) class Author(BaseModel): """ Author database model. Used for storing author profile and personal details. """ __tablename__ = 'author' id = db.Column(db.Integer, primary_key=True, autoincrement=True) first_name = db.Column(db.String(255)) last_name = db.Column(db.String(255)) avatar = db.Column(db.String(255), nullable=True)
from .users import User from .posts import Posts from apps.extensions import db # 添加用户与帖子的收藏中间关联表 表名.字段 collections = db.Table( 'collections', db.Column('user_id', db.Integer, db.ForeignKey('users.id')), db.Column('posts_id', db.Integer, db.ForeignKey('posts.id'))) #一个用户可以收藏多个帖子 #一个帖子可以被多个用户收藏 #多对多我们是通过 中间表来实现