SQlAlchemy:无法确定 parent/child 表之间关系错误消息的连接条件

SQlAlchemy: Could not determine join condition between parent/child tables on relationship error message

我最近用过sqlacodegen

当我尝试 运行 生成的代码时,它收到我无法修复的消息:

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

我想知道是否有人可以解释一下:

这是为导致问题的相关 table 生成的代码:

class Workgrp(Owner):
    __tablename__ = 'workgrp'

    workgrp_id = Column(ForeignKey('owner.owner_id'), primary_key=True)
    workgrp_prntid = Column(Numeric(scale=0, asdecimal=False))
    workgrp_name = Column(String(256))
    workgrp_desc = Column(String(4000))
    workgrp_owner = Column(ForeignKey('usrmst.usrmst_id'))
    workgrp_lstchgtm = Column(DateTime, index=True)
    workgrp_externid = Column(String(20))
    workgrp_profile = Column(Text)
    workgrp_usrmodtm = Column(DateTime)

    usrmst = relationship('Usrmst')


class Usrmst(Owner):
    __tablename__ = 'usrmst'
    __table_args__ = (
        Index('usrmst_ak1', 'usrmst_domain', 'usrmst_name'),
    )

    usrmst_id = Column(ForeignKey('owner.owner_id'), primary_key=True)
    usrmst_domain = Column(String(256))
    usrmst_name = Column(String(256), nullable=False)
    usrmst_fullname = Column(String(1024))
    usrmst_desc = Column(String(4000))
    usrmst_phoneno = Column(String(40))
    usrmst_pagerno = Column(String(40))
    usrmst_email = Column(String(1024))
    usrmst_emailtype = Column(Numeric(scale=0, asdecimal=False))
    secmst_id = Column(ForeignKey('secmst.secmst_id'))
    lngmst_id = Column(ForeignKey('lngmst.lngmst_id'))
    usrmst_password = Column(String(1024))
    usrmst_externid = Column(String(20))
    usrmst_suser = Column(String(1))
    usrmst_lstchgtm = Column(DateTime, index=True)
    usrmst_orapassword = Column(String(144))
    usrmst_wingroup = Column(String(1))
    usrmst_tmpacct = Column(String(1))
    usrmst_profile = Column(Text)
    usrmst_usrmodtm = Column(DateTime)
    usrmst_principal = Column(String(256))
    usrmst_keytab = Column(String(4000))

    lngmst = relationship('Lngmst')
    secmst = relationship('Secmst')

我看过 SQLAlchemy 文档,并尝试使用

但最终收到的消息如下:

以下是 DBeaver 中与原始表相关的一些屏幕截图,基于 Eclipse 的 SQL Explorer 类型插件:

我认为这不会有太大区别,但我正在开发:

我在生成的代码之后添加了一个片段,以及相关的导入..

engine = create_engine("oracle://<dbuser>:<dbpwd>@<host>:<port>/<db>")
Session = sessionmaker(bind=engine)
session = Session()
firstjm = session.query(Jobmst).first()
print(firstjm)

关于 sqlacodegen 生成代码的其他烦人的事情:

我终于找到了语法糖,对确切的术语进行了 Google 高级搜索:


usrmst = relationship('Usrmst', foreign_keys=[workgrp_owner])

我还覆盖了 __repr__ 的默认实现,用于由以下定义的基础 class:

Base = declarative_base()

对此:

import json
def tablerepr(self):
    return "<{}: ({})>".format(self.__table__,
                             json.dumps(
                                dict(
                                    list(
                                        self.__dict__.items()
                                        )[1:]
                                 ), default=str)
                             )
Base.__repr__ = tablerepr

我在掌握 SQLAlchemy 吐出的对象时发现的有用链接:

所以我最终将 tablerepr 方法添加到我的主要生成代码中..

然后在一个单独的 python 脚本中,我通过调整导入生成的代码,并具有如下代码:

engine = create_engine("oracle://<user>:<password>@<host>:<port>/<db>")
inspector = inspect(engine)
Session = sessionmaker(bind=engine)
session = Session()
print(session.query(Jobmst).first())

Jobmst 是 class 代表我导入的 table。这表示 print 然后生成的内容类型,客户数据替换为 ... table 名称,然后是一个包含每个列名称的元组及其数据作为 key/value 对:

<jobmst: ({"jobmst_id": 1590.0, "jobmst_alias": "1590", "jobmst_owner": 67.0, "jobmst_type": 1.0, "jobmst_dirty": "0", "jobmst_desc": null, "jobmst_vars": null, "jobmst_prntid": 1589.0, "jobmst_crttm": "2001-06-18 10:38:19", "jobmst_active": "N", "jobmst_usrmodtm": "2001-06-29 20:27:06", "jobdtl_id": 1591.0, "evntmst_id": null, "bizunit_id": null, "jobmst_lstchgtm": "2001-06-29 20:27:06", "jobmst_evntoffset": null, "jobmst_desc_type": null, "jobmst_runbook": null, "jobmst_name": "....", "jobmst_runbook_type": null, "jobcls_id": 32.0, "jobmst_mode": null, "jobmst_prntname": "...."})>