gpt4 book ai didi

python - 与 MySQL 一起使用的 SQLAlchemy 关系在与 Sql Server 一起使用时会产生错误

转载 作者:行者123 更新时间:2023-11-30 21:48:47 24 4
gpt4 key购买 nike

我有一些与 MySQL 一起使用的 SQLAlchemy ORM 代码,但是当我将底层数据库更改为 Sql Server 时,我收到消息模块“user_group”没有以名称“foreign_keys”注册的映射类

这是其中一个主要类的 ORM 定义:

class User(Base):
__tablename__ = 'user'
__table_args__ = (
{'schema':'admn'}
)

user_id = Column(Integer, autoincrement=True, primary_key=True)
user_name = Column(Unicode(255), unique=True, nullable=False)
_password = Column('password', Unicode(80), nullable=True)
authentication_backend = Column(Unicode(255), nullable=False, default='application_db')

groups = relationship(
"Group",
secondary="user_group",
back_populates="users",
cascade="all", passive_deletes = True)

这是另一个主要类的定义:

class Group(Base):
__tablename__ = 'group'
__table_args__ = (
{'schema': 'admn'}
)

group_id = Column(Integer, autoincrement=True, primary_key=True)
name = Column(Unicode(255), unique=True, nullable=False)
description = Column(Unicode(255), unique=True, nullable=True)

users = relationship(
"User",
secondary="user_group",
back_populates="groups",
cascade="all",
passive_deletes=True)

以及关联它们的二级类:

class UserGroup(Base):
__tablename__ = 'user_group'
__table_args__ = (
{'schema': 'admn'}
)

user_id = Column(Integer, ForeignKey('user.user_id', ondelete="cascade", onupdate="cascade"), primary_key = True)
group_id = Column(Integer, ForeignKey('group.group_id', ondelete="cascade", onupdate="cascade"), primary_key = True)

知道为什么我在 SQL Server 而不是 MySql 中收到此错误消息吗?

迈克尔

相关回溯:

    user = Session.query(User).filter(User.user_name==user_name).first()
File "/home/michaell/projects/ddp_ve/lib/python3.5/site-packages/sqlalchemy/orm/scoping.py", line 153, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "/home/michaell/projects/ddp_ve/lib/python3.5/site-packages/sqlalchemy/orm/session.py", line 1393, in query
return self._query_cls(entities, self, **kwargs)
File "/home/michaell/projects/ddp_ve/lib/python3.5/site-packages/sqlalchemy/orm/query.py", line 140, in __init__
self._set_entities(entities)
File "/home/michaell/projects/ddp_ve/lib/python3.5/site-packages/sqlalchemy/orm/query.py", line 151, in _set_entities
self._set_entity_selectables(self._entities)
File "/home/michaell/projects/ddp_ve/lib/python3.5/site-packages/sqlalchemy/orm/query.py", line 181, in _set_entity_selectables
ent.setup_entity(*d[entity])
File "/home/michaell/projects/ddp_ve/lib/python3.5/site-packages/sqlalchemy/orm/query.py", line 3619, in setup_entity
self._with_polymorphic = ext_info.with_polymorphic_mappers
File "/home/michaell/projects/ddp_ve/lib/python3.5/site-packages/sqlalchemy/util/langhelpers.py", line 767, in __get__
obj.__dict__[self.__name__] = result = self.fget(obj)
File "/home/michaell/projects/ddp_ve/lib/python3.5/site-packages/sqlalchemy/orm/mapper.py", line 1994, in _with_polymorphic_mappers
configure_mappers()
File "/home/michaell/projects/ddp_ve/lib/python3.5/site-packages/sqlalchemy/orm/mapper.py", line 3013, in configure_mappers
mapper._post_configure_properties()
File "/home/michaell/projects/ddp_ve/lib/python3.5/site-packages/sqlalchemy/orm/mapper.py", line 1811, in _post_configure_properties
prop.init()
File "/home/michaell/projects/ddp_ve/lib/python3.5/site-packages/sqlalchemy/orm/interfaces.py", line 184, in init
self.do_init()
File "/home/michaell/projects/ddp_ve/lib/python3.5/site-packages/sqlalchemy/orm/relationships.py", line 1656, in do_init
self._setup_join_conditions()
File "/home/michaell/projects/ddp_ve/lib/python3.5/site-packages/sqlalchemy/orm/relationships.py", line 1731, in _setup_join_conditions
can_be_synced_fn=self._columns_are_mapped
File "/home/michaell/projects/ddp_ve/lib/python3.5/site-packages/sqlalchemy/orm/relationships.py", line 1998, in __init__
self._determine_joins()
File "/home/michaell/projects/ddp_ve/lib/python3.5/site-packages/sqlalchemy/orm/relationships.py", line 2065, in _determine_joins
consider_as_foreign_keys=consider_as_foreign_keys
File "<string>", line 2, in join_condition
File "/home/michaell/projects/ddp_ve/lib/python3.5/site-packages/sqlalchemy/sql/selectable.py", line 964, in _join_condition
a, a_subset, b, consider_as_foreign_keys)
File "/home/michaell/projects/ddp_ve/lib/python3.5/site-packages/sqlalchemy/sql/selectable.py", line 996, in _joincond_scan_left_right
b.foreign_keys,
File "/home/michaell/projects/ddp_ve/lib/python3.5/site-packages/sqlalchemy/ext/declarative/clsregistry.py", line 198, in __getattr__
self.__parent.name, key))
AttributeError: Module 'user_group' has no mapped classes registered under the name 'foreign_keys'

最佳答案

除了将 SQL Server 架构名称作为表参数的一部分提及之外,如:

class UserGroup(Base):
__tablename__ = 'user_group'
__table_args__ = (
{'schema': 'admn'}
)

架构名为“admn”。

您还必须在命名表的 ORM 中的各种字符串中提及架构名称。目前我发现的两个例子:

外键:

    user_id = Column(Integer, ForeignKey('admn.user.user_id', ondelete="cascade", onupdate="cascade"), primary_key = True)

在关系中提及表时,例如辅助表:

  users = relationship(
"User",
secondary="admn.user_group",
back_populates="groups",
cascade="all",
passive_deletes=True)

这是导致我的映射器错误的最后一个地方。当我在 secondary=... 中提到模式名称时,IE 映射器错误就消失了。

关于python - 与 MySQL 一起使用的 SQLAlchemy 关系在与 Sql Server 一起使用时会产生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48230018/

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