<p dir="auto"><strong>Migrated issue, originally created by Alistair Watson</strong></p>
<p dir="auto">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.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="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(&quot;Child&quot;,back_populates=&quot;parent&quot;,secondary=&quot;secondary&quot;)

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(&quot;Parent&quot;,back_populates=&quot;children&quot;,secondary=&quot;secondary&quot;)
    
insp = inspect(Child)

for a in insp.relationships:
    print(a)
    
"><pre class="notranslate"><code class="notranslate">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)
    

</code></pre></div>
<p dir="auto">and the stack trace</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
  File &quot;/home/aw/env3/lib/python3.4/site-packages/sqlalchemy/ext/declarative/clsregistry.py&quot;, line 281, in __call__
    x = eval(self.arg, globals(), self._dict)
  File &quot;&lt;string&gt;&quot;, line 1, in &lt;module&gt;
NameError: name 'secondary' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File &quot;schema_test.py&quot;, line 25, in &lt;module&gt;
    for a in insp.relationships:
  File &quot;/home/aw/env3/lib/python3.4/site-packages/sqlalchemy/util/langhelpers.py&quot;, line 764, in __get__
    obj.__dict__[self.__name__] = result = self.fget(obj)
  File &quot;/home/aw/env3/lib/python3.4/site-packages/sqlalchemy/orm/mapper.py&quot;, line 2265, in relationships
    return self._filter_properties(properties.RelationshipProperty)
  File &quot;/home/aw/env3/lib/python3.4/site-packages/sqlalchemy/orm/mapper.py&quot;, line 2282, in _filter_properties
    configure_mappers()
  File &quot;/home/aw/env3/lib/python3.4/site-packages/sqlalchemy/orm/mapper.py&quot;, line 2866, in configure_mappers
    mapper._post_configure_properties()
  File &quot;/home/aw/env3/lib/python3.4/site-packages/sqlalchemy/orm/mapper.py&quot;, line 1765, in _post_configure_properties
    prop.init()
  File &quot;/home/aw/env3/lib/python3.4/site-packages/sqlalchemy/orm/interfaces.py&quot;, line 184, in init
    self.do_init()
  File &quot;/home/aw/env3/lib/python3.4/site-packages/sqlalchemy/orm/relationships.py&quot;, line 1652, in do_init
    self._process_dependent_arguments()
  File &quot;/home/aw/env3/lib/python3.4/site-packages/sqlalchemy/orm/relationships.py&quot;, line 1677, in _process_dependent_arguments
    setattr(self, attr, attr_value())
  File &quot;/home/aw/env3/lib/python3.4/site-packages/sqlalchemy/ext/declarative/clsregistry.py&quot;, 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 (&quot;name 'secondary' is not defined&quot;). If this is a class name, consider adding this relationship() to the &lt;class '__main__.Child'&gt; class after both dependent classes have been defined."><pre class="notranslate"><code class="notranslate">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 "&lt;string&gt;", line 1, in &lt;module&gt;
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 &lt;module&gt;
    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 &lt;class '__main__.Child'&gt; class after both dependent classes have been defined.
</code></pre></div>
<p dir="auto"><strong>Migrated issue, originally created by Anonymous</strong></p>
<p dir="auto">In cases where a bi-directional one-to-one relationship is required a simple enhancement could be made to automatically set uselist=False in some cases. If the referencing column (i.e. the one with the FK object passed in) is unique (either from a unique or primary constraint) then the nature of the relationship is easily deduced.</p>