gpt4 book ai didi

python - SQLAlchemy 多对多关系参数

转载 作者:行者123 更新时间:2023-11-29 12:58:31 24 4
gpt4 key购买 nike

我正在使用 SQLAlchemy ORM 获取属于某个用户的角色,但出现以下错误

Could not determine join condition between parent/child tables on relationship AppRole.users - there are multiple foreign key paths linking the tables via secondary table 'app_user_role'. Specify the 'foreign_keys' argument, providing a list of those columns which should be counted as containing a foreign key reference from the secondary table to each of the parent and child tables.

表格定义为:

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)

问题是,我还有其他表,我在其中定义了其他多对多关系参数,并且一切正常。在这里,尽管我不断收到该错误。 SO 上还有其他类似的问题,但尝试了不同的建议但没有运气。我尝试按照建议的错误进行操作,并使用了我之前的一个问题 here on SO但即使放置 foreign_keys 选项似乎也不起作用,尽管我必须承认我真的在猜测放置它的位置。

SQLAlchemy 版本 ==1.0.12,Postgresql 9.5

最佳答案

如错误消息所述,您有多个外键将 AppRole 链接到 AppUser,即 AppUserRole.user_idAppUserRole .deleted_by,和AppUserRole.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")

关于python - SQLAlchemy 多对多关系参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36627804/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com