SQLAlchemy ForeignKey找不到表 [英] SQLAlchemy ForeignKey can't find table

查看:172
本文介绍了SQLAlchemy ForeignKey找不到表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试实例化ConsumerAdvice类时遇到此错误.

Getting this error when I try to instantiate the ConsumerAdvice class.

Foreign key associated with column 'tbConsumerAdvice.ConsumerAdviceCategory_ID' 
could not find table 'tbConsumerAdviceCategories' with which to generate a
foreign key to target column 'ID_ConsumerAdviceCategories'

class ConsumerAdviceCategory(Base):
    __tablename__ = 'tbConsumerAdviceCategories'
    __table_args__ = {'schema':'dbo'}
    ID_ConsumerAdviceCategories = Column(INTEGER, Sequence('idcac'),\
            primary_key=True)
    Name = Column(VARCHAR(50), nullable=False)

    def __init__(self,Name):
        self.Name = Name

    def __repr__(self):
        return "< ConsumerAdviceCategory ('%s') >" % self.Name

class ConsumerAdvice(Base):
    __tablename__ = 'tbConsumerAdvice'
    __table_args__ = {'schema':'dbo'}
    ID_ConsumerAdvice = Column(INTEGER, Sequence('idconsumeradvice'),\
            primary_key=True)
    ConsumerAdviceCategory_ID = Column(INTEGER,\
            ForeignKey('tbConsumerAdviceCategories.ID_ConsumerAdviceCategories'))
    Name = Column(VARCHAR(50), nullable=False)
    Category_SubID = Column(INTEGER)

    ConsumerAdviceCategory = relationship("ConsumerAdviceCategory",\
            backref=backref('ConsumerAdvices'))

    def __init__(self,Name):
        self.Name = Name

    def __repr__(self):
        return "< ConsumerAdvice ('%s') >" % self.Name

推荐答案

定义包含架构的FK:dbo.tbConsumerAdviceCategories.ID_ConsumerAdviceCategories

Define the FK including schema: dbo.tbConsumerAdviceCategories.ID_ConsumerAdviceCategories

这篇关于SQLAlchemy ForeignKey找不到表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆