title: sqlalchemy + flask: class is not defined
body: <p>i'm using sqlalchemy + alembic + Flask and i can't map circular classes. </p>
  
  <p>apps/users/models.py:</p>
  
  <pre><code>class User(Base):
      __tablename__ = 'users'
  
      id = Column(Integer, primary_key=True)
      email = Column(String)
      password = Column(String)
      session = relationship("Session", back_populates='user', cascade='all,delete', lazy='dynamic')
  
      notes = relationship('Note2User', back_populates='user', cascade='all,delete', lazy='dynamic')
  </code></pre>
  
  <p>apps/notes/models.py:</p>
  
  <pre><code>class Note2User(Base):
      __tablename__ = 'notes_users_m2m'
  
      id = Column(Integer, primary_key=True)
  
      user_id = Column(Integer, ForeignKey('users.id', ondelete='CASCADE'), nullable=False)
      user = relationship('User', back_populates='notes')
      note_id = Column(Integer, ForeignKey('notes.id', ondelete='CASCADE'), nullable=False)
      note = relationship('Note', back_populates='users')
  </code></pre>
  
  <p>Table Note2User made for m2m relationship User &lt;-> Notes, but when i start app and done some request, gets error:</p>
  
  <blockquote>
    <p>InvalidRequestError: When initializing mapper Mapper|User|users,
    expression 'Note2User' failed to locate a name ("name 'Note2User' is
    not defined"). If this is a class name, consider adding this
    relationship() to the  class after
    both dependent classes have been defined.</p>
  </blockquote>
  
  <p>Initializing db in db/<strong>init</strong>.py: (dunder name)</p>
  
  <pre><code>from sqlalchemy import create_engine, MetaData
  from sqlalchemy.ext.declarative import declarative_base
  from sqlalchemy.orm import Session
  
  engine = create_engine('postgresql+psycopg2://server:12345@localhost:5432/test')
  
  Base = declarative_base()
  
  meta = MetaData()
  meta.reflect(bind=engine)
  
  db_session = Session(bind=engine)
  </code></pre>