SQLAlchemy多对多关系参数

时间:2016-04-14 15:37:34

标签: python postgresql sqlalchemy

使用SQLAlchemy ORM获取属于某个用户的角色但是收到以下错误

  

无法确定父/子表之间的连接条件   关系AppRole.users - 有多个外键路径   通过辅助表'app_user_role'链接表。指定   'foreign_keys'参数,提供那些列的列表   应被视为包含来自的外键引用   每个父表和子表的辅助表。

表格定义为:

class AppUserRole(Base_Write):
    __tablename__ = 'app_user_role'
    id = Column(Integer, primary_key=True)
    created_at = Column(DateTime, server_default=text('NOW()'), nullable=False)
    created_by = Column(Integer, ForeignKey('app_user.id'), nullable=False)
    is_deleted = Column(Boolean, server_default='0', nullable=False)
    date_deleted = Column(DateTime)
    deleted_by = Column(Integer, ForeignKey('app_user.id'))
    user_id = Column(Integer, ForeignKey('app_user.id'), nullable=False)
    role_id = Column(Integer, ForeignKey('app_role.id'), nullable=False)
    date_assigned = Column(DateTime, nullable=False)


class AppRole(Base_Write):
    __tablename__ = 'app_role'
    id = Column(Integer, primary_key=True)
    created_at = Column(DateTime, server_default=text('NOW()'), nullable=False)
    created_by = Column(Integer)
    is_deleted = Column(Boolean, server_default='0', nullable=False)
    date_deleted = Column(DateTime)
    deleted_by = Column(Integer, ForeignKey('app_user.id'))
    role_classification_id = Column(Integer, ForeignKey('app_role_classification.id'), nullable=False)
    role_code = Column(String(10), nullable=False, unique=True)
    role_name = Column(String(30), nullable=False)

    users = relationship("AppUser", secondary="app_user_role", back_populates="app_role")


class AppUser(Base_Write):
    __tablename__ = 'app_user'
    id = Column(Integer, primary_key=True)
    created_at = Column(DateTime, server_default=text('NOW()'), nullable=False)
    created_by = Column(Integer, ForeignKey('app_user.id'), nullable=False)
    is_deleted = Column(Boolean, server_default='0', nullable=False)
    date_deleted = Column(DateTime)
    deleted_by = Column(Integer, ForeignKey('app_user.id'))
    username = Column(String(50), nullable=False, unique=True)

    roles = relationship("AppRole", secondary="app_user_role", back_populates="app_user")

正在运行的查询是:

for app_user in session.query(AppUser).\
filter(AppUser.is_deleted == False).filter(AppUser.id == user_id):
    #print the names of the roles assigned
    print(app_user.roles.role_name)

问题是,我有其他表格,我已经定义了其他多对多关系参数,一切正常。虽然我不断收到这个错误。在这里还有其他类似的问题,但尝试了不同的建议,但没有运气。我已经尝试通过建议的错误并使用我之前的问题here on SO,但即使放置foreign_keys选项似乎也不起作用,虽然我必须承认我真的在猜测放在哪里。

SQLAlchemy version == 1.0.12,Postgresql 9.5

1 个答案:

答案 0 :(得分:2)

正如错误消息所示,您有多个外键将AppRoleAppUser相关联,即AppUserRole.user_idAppUserRole.deleted_byAppUserRole.created_by,所以你需要指定foreign_keys参数:

class AppRole(Base):
    ...
    users = relationship("AppUser", secondary="app_user_role", 
                         foreign_keys=[AppUserRole.user_id, AppUserRole.role_id],
                         back_populates="roles")


class AppUser(Base):
    ...
    roles = relationship("AppRole", secondary="app_user_role",
                         foreign_keys=[AppUserRole.user_id, AppUserRole.role_id],
                         back_populates="users")