sqlalchemy.exc.InvalidRequestError表达式' memberproductmapping'找不到名字

时间:2017-09-15 21:22:42

标签: python flask sqlalchemy foreign-keys relationship

base.py

from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()

Rules.py

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])

Config.py

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
        }

MemberProductMapping.py

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])

当我试图通过

获取规则列表时
result = session.query(Rules).order_by(Rules.ruleenineid)

我收到了这个错误

  

sqlalchemy.exc.InvalidRequestError:初始化映射器时Mapper | MemberProductMapping | memberproductmapping,expression' config'找不到姓名("名称' config'未定义")。如果这是一个类名,请考虑在定义了两个依赖类之后将此关系()添加到类中。

我不确定问题出在哪里。当我为简单的表(没有FK引用)尝试类似的CRUD操作时,它运行正常。

我也附上了我的架构图 enter image description here

0 个答案:

没有答案