zoukankan      html  css  js  c++  java
  • 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")
     
  • 相关阅读:
    KMP
    C. Three Base Stations
    Calculation 2 [HUD 3501]
    组合数 com(n,r)
    Keep Deleting [zoj 3643]
    LightSwitch V1.0 正式发布,配套控件收集 !
    微软 Lightswitch 学习研讨 QQ群:97010590
    IIS 7开发与管理完全参考手册
    电影 下载 地址收集
    2011年A股中报十大筹码集中王 (收藏)
  • 原文地址:https://www.cnblogs.com/pythonwenma/p/12657392.html
Copyright © 2011-2022 走看看