【发布时间】:2011-11-25 19:18:28
【问题描述】:
当我尝试实例化 ConsumerAdvice 类时出现此错误。
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
【问题讨论】:
-
不知道为什么,但是如果您从
__table_args__中删除schema,它可以工作(用SQLite 测试)。您使用哪种 RDBMS? -
... 尝试定义 FK 包括架构:
dbo.tbConsumerAdviceCategories.ID_ConsumerAdviceCategories -
@van 这是 MSSQL,将架构添加到 FK 定义中有效!很多 KUDOS.. 你可以添加答案,以便我可以将其标记为已回答?
标签: python foreign-keys sqlalchemy