class User(db.Model): """ User Model for storing user related details """ __tablename__ = "User" id = db.Column(db.Integer, primary_key=True, autoincrement=True) username = db.Column(db.String(255), unique=True, nullable=False) useremail = db.Column(db.String(255), nullable=False) password = db.Column(db.String(255), nullable=False)
class BlogPost(db.Model): __tablename__ = "blog_posts" id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(250), unique=True, nullable=False) subtitle = db.Column(db.String(250), nullable=False) date = db.Column(db.String(250), nullable=False) body = db.Column(db.Text, nullable=False) img_url = db.Column(db.String(250), nullable=False) author_id = db.Column(db.Integer(), db.ForeignKey('user.id')) author = db.relationship("User", back_populates="blog_post")
class User(UserMixin, db.Model): __tablename__ = "user" id = db.Column(db.Integer, primary_key=True, nullable=False, auto_increment=True) user_name = db.Column(db.String(length=30), nullable=False, unique=True) email = db.Column(db.String(length=50), nullable=False, unique=True) password_hash = db.Column(db.String(length=60), nullable=False) blog_post = db.relationship('BlogPost', back_populates="author") comments = db.relationship("Comments", back_populates="comment_author")
class User(db.Model): """创建用户表""" id = db.Column(db.Integer(), primary_key=True) # 整型主键 username = db.Column(db.String(30)) phone = db.Column(db.String(11), nullable=False) password = db.Column(db.String(30), nullable=False) # 多对一关系,如果想要一对一,添加uselist=False # backref:反向引用,在QQOauth类型上声明一个新属性,用于QQOauth的实例通过该属性访问该模型实例 # lazy决定了sqlalchemy何时从数据库中加载数据, dynamic懒加载,返回查询对象,可进一步过滤后hit数据库 qq_auth = db.relationship('QQOauth', backref='user', lazy='dynamic') def __init__(self, username, phone, password): self.username = username self.phone = phone self.password = password def __repr__(self): return f"<[User] username:{self.username}, phone: {self.phone}"
class WeiBoOauth(db.Model): """创建微博第三方登录""" # 添加外键约束 id = db.Column(db.Integer, primary_key=True) wb_user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) openid = db.Column(db.String(128), nullable=False)
class QQOauth(db.Model): """创建QQ第三方登录""" # 添加外键约束 id = db.Column(db.Integer, primary_key=True) # 创建外键,参照主表的id qq_user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) openid = db.Column(db.String(128), nullable=False)
class SessionModel(db.Model): id = db.Column(db.Integer, primary_key=True) access_token = db.Column(db.String(), unique=True) refresh_token = db.Column(db.String(), unique=True) @property def get_access_token(self): return self.access_token @property def get_refresh_token(self): return self.refresh_token def save(self): db.session.add(self) db.session.commit() @staticmethod def delete(): db.session.query(SessionModel).delete() db.session.commit()