[Sqlalchemy-tickets] Issue #3978: inspect relationships fails on many to many tables with schema se
Brought to you by:
zzzeek
From: Alistair W. <iss...@bi...> - 2017-05-05 11:58:54
|
New issue 3978: inspect relationships fails on many to many tables with schema set https://bitbucket.org/zzzeek/sqlalchemy/issues/3978/inspect-relationships-fails-on-many-to Alistair Watson: The following code fails to print the relationships when I set the schema argument of Metadata and when inspecting a table with a many to many relationship. If the schema argument is removed or the tables have a different relationship such as one to many then the relationship attribute works. I am using the latest version of SQLAlchemy (1.1.9) and Python 3.4.5. I also tried it using an old version of SQLAlchemy (0.9.10) and the problem exists there too. ``` #!python from sqlalchemy import MetaData, inspect, Column, ForeignKey, Integer from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import relationship metadata = MetaData(schema='test_schema') Base = declarative_base(metadata=metadata) class Parent(Base): __tablename__ = 'parent' id = Column(Integer, primary_key=True) children = relationship("Child",back_populates="parent",secondary="secondary") class Secondary(Base): __tablename__ = 'secondary' fk_parent = Column(Integer, ForeignKey('parent.id'), primary_key=True) fk_child = Column(Integer, ForeignKey('child.id'), primary_key=True) class Child(Base): __tablename__ = 'child' id = Column(Integer, primary_key=True) parent = relationship("Parent",back_populates="children",secondary="secondary") insp = inspect(Child) for a in insp.relationships: print(a) ``` and the stack trace ``` #!python Traceback (most recent call last): File "/home/aw/env3/lib/python3.4/site-packages/sqlalchemy/ext/declarative/clsregistry.py", line 281, in __call__ x = eval(self.arg, globals(), self._dict) File "<string>", line 1, in <module> NameError: name 'secondary' is not defined During handling of the above exception, another exception occurred: Traceback (most recent call last): File "schema_test.py", line 25, in <module> for a in insp.relationships: File "/home/aw/env3/lib/python3.4/site-packages/sqlalchemy/util/langhelpers.py", line 764, in __get__ obj.__dict__[self.__name__] = result = self.fget(obj) File "/home/aw/env3/lib/python3.4/site-packages/sqlalchemy/orm/mapper.py", line 2265, in relationships return self._filter_properties(properties.RelationshipProperty) File "/home/aw/env3/lib/python3.4/site-packages/sqlalchemy/orm/mapper.py", line 2282, in _filter_properties configure_mappers() File "/home/aw/env3/lib/python3.4/site-packages/sqlalchemy/orm/mapper.py", line 2866, in configure_mappers mapper._post_configure_properties() File "/home/aw/env3/lib/python3.4/site-packages/sqlalchemy/orm/mapper.py", line 1765, in _post_configure_properties prop.init() File "/home/aw/env3/lib/python3.4/site-packages/sqlalchemy/orm/interfaces.py", line 184, in init self.do_init() File "/home/aw/env3/lib/python3.4/site-packages/sqlalchemy/orm/relationships.py", line 1652, in do_init self._process_dependent_arguments() File "/home/aw/env3/lib/python3.4/site-packages/sqlalchemy/orm/relationships.py", line 1677, in _process_dependent_arguments setattr(self, attr, attr_value()) File "/home/aw/env3/lib/python3.4/site-packages/sqlalchemy/ext/declarative/clsregistry.py", line 292, in __call__ (self.prop.parent, self.arg, n.args[0], self.cls) sqlalchemy.exc.InvalidRequestError: When initializing mapper Mapper|Child|child, expression 'secondary' failed to locate a name ("name 'secondary' is not defined"). If this is a class name, consider adding this relationship() to the <class '__main__.Child'> class after both dependent classes have been defined. ``` |