• flask_sqlalchemy中关系选项的问题


    1. 再今兴多对多的关系设定的时候,我们会绑定中间表

    中间表:
    Users_roles = db.Table("users_roles", db.Model.metadata,
    db.Column("user_id", db.Integer, db.ForeignKey("user.id"), primary_key=True),
    db.Column("role_id", db.Integer, db.ForeignKey("role.id"), primary_key=True)
    )
    class User(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, db.Sequence('user_id_seq'), primary_key=True)
    user_id = db.Column(db.String(80), unique=True) # 员工编号
    name = db.Column(db.String(80), nullable=False) # 员工姓名
    # job_title = db.Column(db.String(80), default="engineer")
    # mgr_title = db.Column(db.String(80), default="employee")
    _password = db.Column(db.String(128), nullable=False) # 密码, BCRYPT
    active = db.Column(db.Boolean(), default=False)
    user_type = db.Column(db.String(80), default="user")
    status = db.Column(db.String(80), default="deactivate")
    department_id = db.Column(db.Integer, db.ForeignKey('department.id'))
    dr = db.Column(db.Integer, default=0)
    creator_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    creator = db.relationship("User", backref='created_by', remote_side=[id], foreign_keys=[creator_id])
    created_at = db.Column(db.DateTime, default=datetime.now)
    modifyuser_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    modifyuser = db.relationship("User", backref='modified_by', remote_side=[id], foreign_keys=[modifyuser_id])
    modify_at = db.Column(db.DateTime)
    roles = relationship("Role", secondary=Users_roles, backref="users")
      # 再为secondary赋值的时候,不加引号赋的是类名,加引号赋得是表名
    class Role(db.Model):
    __tablename__ = 'role'
    id = db.Column(db.Integer, Sequence('role_seq'), primary_key=True)
    name = db.Column(db.String(80), nullable=False)
    code = db.Column(db.String(80), nullable=False)
    dr = db.Column(db.Integer, default=0)
    creator_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    creator = db.relationship("User", foreign_keys=creator_id)
    created_at = db.Column(db.DateTime, default=datetime.now)
    modifyuser_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    modifyuser = db.relationship("User", foreign_keys=modifyuser_id)
    modify_at = db.Column(db.DateTime)
    privileges = relationship("Privilege", secondary=Roles_pris, backref="roles")
     
  • 相关阅读:
    分层图(了解一下,下一道比较好做)
    图论---The Captain
    数论
    NOIp复习计划
    20201116 Day4 卢卡斯定理
    20201113 Day3 斜率优化
    20201110Day2 分块
    20201030 day50 复习13:逆元、裴蜀定理
    20201030day50 模板全掌握
    20201029 day49 模拟(十八)
  • 原文地址:https://www.cnblogs.com/pythonwenma/p/12657392.html
Copyright © 2020-2023  润新知