title: sqlalchemy.exc.InvalidRequestError expression 'memberproductmapping' failed to locate a name
body: <p>base.py</p>
  
  <pre><code>from sqlalchemy.ext.declarative import declarative_base
  Base = declarative_base()
  </code></pre>
  
  <p>Rules.py</p>
  
  <pre><code>from sqlalchemy import Column, Integer, String
  from sqlalchemy import ForeignKey
  from sqlalchemy.orm import relationship
  from datasource.driver.base import Base
  
  
  class Rules(Base):
      __tablename__ = 'ruleengine'
  
      ruleenineid = Column(Integer, primary_key=True)
      bridge1 = Column(Integer, ForeignKey('memberproductmapping.bridgeid'))
      bridge2 = Column(Integer, ForeignKey('memberproductmapping.bridgeid'))
      # TODO do we have to keep track of table name and normalize it as its possible for integrity violation?
      table1 = Column(String(100, 'SQL_Latin1_General_CP1_CI_AS'))
      table2 = Column(String(100, 'SQL_Latin1_General_CP1_CI_AS'))
      # TODO do we have to keep track of column name and normalize it as its possible for integrity violation?
      column1 = Column(String(100, 'SQL_Latin1_General_CP1_CI_AS'))
      column2 = Column(String(100, 'SQL_Latin1_General_CP1_CI_AS'))
      entityid = Column(Integer, ForeignKey('entities.entityid'))
  
      memberproductmapping = relationship('memberproductmapping', foreign_keys=[bridge1, bridge2])
      entities = relationship('entities', foreign_keys=[entityid])
  </code></pre>
  
  <p>Config.py</p>
  
  <pre><code>from sqlalchemy import Column, Integer, String
  from sqlalchemy.ext.declarative import declarative_base
  from datasource.driver.base import Base
  
  
  class Config(Base):
      __tablename__ = 'config'
  
      config_id = Column(Integer, primary_key=True)
      db_name = Column(String(100, 'SQL_Latin1_General_CP1_CI_AS'))
      server_name = Column(String(100, 'SQL_Latin1_General_CP1_CI_AS'))
  
      def serialize(self):
          return {
              'config_id': self.config_id,
              'db_name': self.db_name,
              'server_name': self.server_name
          }
  </code></pre>
  
  <p>MemberProductMapping.py</p>
  
  <pre><code>from sqlalchemy import Column, Integer, ForeignKey
  from sqlalchemy.orm import relationship
  from sqlalchemy.ext.declarative import declarative_base
  from datasource.driver.base import Base
  
  
  
  class MemberProductMapping(Base):
      __tablename__ = 'memberproductmapping'
  
      bridgeid = Column(Integer, primary_key=True)
      memberid = Column(Integer, ForeignKey('member.memberid'))
      productid = Column(Integer, ForeignKey('product.productid'))
      configid = Column(Integer, ForeignKey('config.configid'))
  
      config = relationship('config', foreign_keys=[configid])
      member = relationship('member', foreign_keys=[memberid])
      product = relationship('croduct', foreign_keys=[productid])
  </code></pre>
  
  <p>When I try to get list of rules by </p>
  
  <pre><code>result = session.query(Rules).order_by(Rules.ruleenineid)
  </code></pre>
  
  <p>I got this error</p>
  
  <blockquote>
    <p>sqlalchemy.exc.InvalidRequestError: When initializing mapper Mapper|MemberProductMapping|memberproductmapping, expression 'config' failed to locate a name ("name 'config' 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>I am not sure where is the problem. When I tried similar CRUD operation for simple tables(without FK reference) it worked fine.</p>
  
  <p>I attached my schema diagram as well
  <a href="https://i.stack.imgur.com/1sS0p.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1sS0p.png" alt="enter image description here"></a></p>