gpt4 book ai didi

python - SQLAlchemy Automap 反向引用错误

转载 作者:太空宇宙 更新时间:2023-11-03 11:46:44 26 4
gpt4 key购买 nike

我是 SQLAlchemy(通常是 ORM)的新手,我正在尝试将现有的应用程序转移到 SQLAlchemy,以便我们可以将一些代码复杂性从当前现有(并且更新乏味)查询转移到 Python。不幸的是,我在数据库反射后立即收到错误。虽然我可以直接查询表,但我并不能直接访问类或类之间的关系。下面是我正在尝试做的一个大致最小的例子。

现有的 postgres 表:

dev=> \d+ gmt_file
Table "public.gmt_file"
Column | Type | Modifiers | Storage | Stats target | Description
-----------+--------------+-----------+----------+--------------+-------------
file_id | integer | not null | plain | |
a | integer | | plain | |
b | integer | | plain | |
Indexes:
"gmt_file_pk" PRIMARY KEY, btree (file_id)
Foreign-key constraints:
"gmt_file_a_fk" FOREIGN KEY (a) REFERENCES cmn_user(user_id)
"gmt_file_b_fk" FOREIGN KEY (b) REFERENCES cmn_user(user_id)

SQLAlchemy 应用程序(最小示例):

from sqlalchemy import create_engine
from sqlalchemy.orm import Session,Mapper
from sqlalchemy.ext.automap import automap_base

engine = create_engine('postgresql://user:pass@localhost:5432/dev')
Base = automap_base()
Base.prepare(engine, reflect=True)
session = Session(engine,autocommit=True)

session.query(Base.classes.gmt_file).all()

据我所知,这会引发反向引用错误,因为 ab 与不同表中的同一字段具有外键关系(这经常发生在现有的数据库中)。我尝试了多种处理此错误的方法,包括创建自定义命名函数(name_for_scalar_relationship()name_for_collection_relationship()),但均无济于事。在 SQLAlchemy 中,是否有一种标准的方法来处理这个问题,或者在反射期间禁用 backref 创建?

最终目标是以自动化方式反射(reflect)数据库,而不必为当前存在的数百个表编写自定义名称映射,但我不知道该怎么做。任何帮助表示赞赏。

谢谢

最佳答案

当多个外键引用同一列时,使用自动映射似乎会发生属性名称冲突。

Base.prepare 允许参数 name_for_scalar_relationshipname_for_collection_relationship,它们采用用于生成属性名称的函数。 (请参阅 AutomapBase.prepare()name_for_collection_relationship() 的文档)我能够通过定义自己的函数来解决 backref 错误。

修改您的最小示例:

from sqlalchemy import create_engine
from sqlalchemy.orm import Session,Mapper
from sqlalchemy.ext.automap import automap_base, name_for_collection_relationship

engine = create_engine('postgresql://user:pass@localhost:5432/dev')
Base = automap_base()

def _name_for_collection_relationship(base, local_cls, referred_cls, constraint):
if constraint.name:
return constraint.name.lower()
# if this didn't work, revert to the default behavior
return name_for_collection_relationship(base, local_cls, referred_cls, constraint)

Base.prepare(engine, reflect=True, name_for_collection_relationship=_name_for_collection_relationship)
session = Session(engine,autocommit=True)

session.query(Base.classes.gmt_file).all()

这应该有望让您的类具有属性名称 gmt_file_a_fkgmt_file_b_fk

这个方法对我有用。如果它不起作用,您也可以尝试类似地重新定义 name_for_scalar_relationship()

如果您想完全覆盖类,则必须确保正确定义列和关系。例如:

from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy.orm import relationship

class GmtFile(Base):
__tablename___ = 'gmt_file'

file_id = Column('file_id', Integer, primary_key=True)

a = Column('a', Integer, ForeignKey('CmnUser.user_id', name='gmt_file_a'))
b = Column('b', Integer, ForeignKey('CmnUser.user_id', name='gmt_file_b'))
# you'll need to define the class CmnUser as well

# these variable names need to be the same as the ForeignKey names above
gmt_file_a = relationship('CmnUser', foreign_keys=[a])
gmt_file_b = relationship('CmnUser', foreign_keys=[b])

关于python - SQLAlchemy Automap 反向引用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37797140/

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