class Answer(db.Model): __tablename__ = 'answer' id = db.Column(db.Integer, primary_key=True, autoincrement=True) content = db.Column(db.Text, nullable=False) create_time = db.Column(db.DateTime, default=datetime.now) question_id = db.Column(db.Integer, db.ForeignKey('question.id')) author_id = db.Column(db.Integer, db.ForeignKey('user.id')) question = db.relationship('Question', backref=db.backref('answers', order_by=id.desc())) author = db.relationship('User', backref=db.backref('answers'))
class ArticleType(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) type_name = db.Column(db.String(20), nullable=False) articles = db.relationship('Article', backref='type') __table_rgs = {'extend_existing': True, 'mysql_charset': 'utf-8'} def __str__(self): return self.type_name
class User(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) username = db.Column(db.String(32), nullable=False) password = db.Column(db.String(101), nullable=False) phone = db.Column(db.String(11), nullable=False, unique=True) email = db.Column(db.String(30)) icon = db.Column(db.String(100)) is_delete = db.Column(db.Boolean, default=False) register_time = db.Column(db.DateTime, default=datetime.datetime.now) # 增加一个字段,不会出现在数据库中,relationship()是在view和template中体现价值的 # backref:反向引用,通过文章找user # lay决定了sqlalchemy什么时候从数据库中加载数据,dynamic在有多条数据的时候特别有用,不是直接加载这些数据 # 默认是select articles = db.relationship('Article', backref='user') comments = db.relationship('Comment', backref='user') __table_args__ = {'extend_existing': True, 'mysql_charset': 'utf8'} def __str__(self): return self.username
class Question(db.Model): __tablename__ = 'question' id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String(100), nullable=False) content = db.Column(db.Text, nullable=False) # datetime.now() 获取服务器第一次运行的时间 # datetime.now 获取记录创建时间 create_time = db.Column(db.DateTime, default=datetime.now) author_id = db.Column(db.Integer, db.ForeignKey('user.id')) author = db.relationship('User', backref=db.backref('questions'))
class MessageBoard(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) content = db.Column(db.String(255), nullable=False) mdatetime = db.Column(db.DateTime, default=datetime.datetime.now) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) user = db.relationship('User', backref='messages') __table__args = {'extend_existing': True, 'mysql_charset': 'utf-8'} def __str__(self): return self.content
class Goods(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) goods_name = db.Column(db.String(100), nullable=False) price = db.Column(db.Float, nullable=False) # 因为用户和商品是多对多关系,无法直接添加外键,所以要依赖第三张关系表来建立relationship,secondary=第三张表表名 # relationship里的backref的值可以随便起,用来反向引用 users = db.relationship('User', backref='goods_list', secondary='user_goods') __table_args__ = {'extend_existing': True, 'mysql_charset': 'utf8'} def __str__(self): return self.goods_name
class AboutMe(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) content = db.Column(db.BLOB, nullable=False) pdatetime = db.Column(db.DateTime, default=datetime.datetime.now) # 与用户建立联系 user_id = db.Column( db.Integer, db.ForeignKey('user.id'), unique=True) # unique没有设置成True,查询出来的话会是一个列表,flask里的外键都是一对多 user = db.relationship('User', backref='about') __table__args = {'extend_existing': True, 'mysql_charset': 'urf-8'} def __str__(self): return self.user
class Article(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String(50), nullable=False) content = db.Column(db.BLOB, nullable=False) publish_date = db.Column(db.DateTime, default=datetime.datetime.now) click_num = db.Column(db.Integer, default=0) save_num = db.Column(db.Integer, default=0) love_num = db.Column(db.Integer, default=0) # 建立外键关系 user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) type_id = db.Column(db.Integer, db.ForeignKey('article_type.id'), nullable=False) comments = db.relationship('Comment', backref='article') __table_args = {'extend_existing': True, 'mysql_charset': 'utf-8'} def __str__(self): return self.title