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

时间:2018-01-12 16:11:34

标签: python mysql sql-server sqlalchemy

我有一些SQLAlchemy ORM代码与MySQL一起使用但是当我将底层数据库更改为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'

1 个答案:

答案 0 :(得分:0)

除了在表args中提及SQL Server模式名称之外,如:

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

架构名为&#34; admn&#34;。

您还必须在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) 

这是造成我的映射器错误的最后一个地方。我一提到辅助设备中的模式名称= IE,映射器错误就消失了。