One-to-One relationship SQLAlchemy with multiple files
<p>i have a problem with my relationship(...) and i don't understand why.</p>

<p>The error : </p>

<p>"InvalidRequestError: When initializing mapper Mapper|User|users, expression 'BannedUser' failed to locate a name ("name 'BannedUser' is not defined"). If this is a class name, consider adding this relationship() to the  class after both dependent classes have been defined."</p>

<p>This is the code :</p>

<p>User model</p>

<pre><code>from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import relationship

from .base import Base as BaseModel


class User(BaseModel, declarative_base()):
     __tablename__ = 'users'

     id = Column(Integer, primary_key=True)
     username = Column(String(16))
     password = Column(String(16))
     nickname = Column(String(16))
     secret_question = Column(String(50))
     secret_answer = Column(String(50))
     role = Column(Integer)
     is_banned = relationship("BannedUser", uselist=False, back_populates='users')
</code></pre>

<p>Banned user model :</p>

<pre><code>from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey
from sqlalchemy.orm import relationship

from .base import Base as BaseModel


class BannedUser(BaseModel, declarative_base()):
     __tablename__ = 'banned_users'

     id = Column(Integer, primary_key=True)
     user_id = Column(Integer, ForeignKey('users.id'))
     user = relationship('User', back_populates='banned_users')
     reason = Column(String)
     time_end = Column(DateTime)
</code></pre>

<p>I tried class directly instead of class string but not work too...</p>

<p>I tried 'User.id' instead of 'users.id' same thing...</p>

<p>I don't know what to do :(</p>

<p>Thank u in advance for the help, and sorry for language i'm french ^^</p>

<p>On the <code>back_populates</code> (<a href="http://docs.sqlalchemy.org/en/latest/orm/relationship_api.html#sqlalchemy.orm.relationship.params.back_populates" rel="nofollow">doc</a>) you need to specify the property which is related on the other end of the relationship, while you specified the table name ... of the same entity ...</p>

<p>I would recommend doing it with <code>back_ref</code> (<a href="http://docs.sqlalchemy.org/en/latest/orm/relationship_api.html#sqlalchemy.orm.relationship.params.backref" rel="nofollow">doc</a>), which takes care of the other end related property (so you don't need to specify the <code>user</code> property on <code>BannedUser</code>, or the other way around), something like:</p>

<pre><code>...

class User(BaseModel, declarative_base()):
    __tablename__ = 'users'

    ...
    is_banned = relationship("BannedUser", uselist=False, back_ref='user')
    ...
</code></pre>

<p>But if you still want to do it with <code>back_populates</code>, you should do something like this:</p>

<pre><code>...

class User(BaseModel, declarative_base()):
    __tablename__ = 'users'

    ...
    is_banned = relationship("BannedUser", uselist=False, back_populates='user')
    ...

class BannedUser(BaseModel, declarative_base()):
    __tablename__ = 'banned_users'

    ...
    user = relationship('User', back_populates='is_banned')
    ...
</code></pre>

python sqlalchemy foreign-keys relationship