0

我正在努力将常规模型与通用关联表链接起来

Flask-SQLAlchemy 2.5.1
SQLAlchemy 1.3.24
SQLAlchemy-Utils 0.36.8

有一个常规模型 - 仪表板,
一个只有 id 和 name 列的角色模型
一个通用关联表 (ObjectRoles),将任何常规模型链接到角色模型 - ManyToMany

from flask_appbuilder import Model
# the Model inherit from DefaultMeta of flask-sqlalchemy

class ObjectRoles(Model):  
    __tablename__ = "object_roles"
    id = Column(Integer, primary_key=True)
    role_id = Column("role_id", Integer, ForeignKey("ab_role.id"), nullable=False)
    object_id = Column(Integer, nullable=False)   
    object_type = Column(String(255), nullable=False)
    object = generic_relationship(object_type, object_id)  # not sure if is required 
    role = relationship(Role)                              # not sure if is required 


class Dashboard(Model):
    __tablename__ = "dashboards"
    id = Column(Integer, primary_key=True)
    roles = # Don't know what to do here, I've tried many alternatives

    # roles = relationship(ObjectRoles,
    #                       primaryjoin="and_(Dashboard.id==ObjectRoles.object_id, "
    #                                   "ObjectRoles.object_type=='Dashboard')",
    #                       )


我需要能够获取诸如主连接之类的角色并能够将新角色插入仪表板,因此在幕后它将在 object_roles 中创建一个新行

如果关联表不是通用的,但具有仪表板的外键,则没有问题

有人吗?

4

0 回答 0