【发布时间】:2014-12-07 09:36:46
【问题描述】:
我已经阅读了所有这些 (https://stackoverflow.com/search?q=sqlalchemy.exc.NoReferencedTableError%3A)、Flask-appbuilder 文档、sqlalchemy 文档和 Flask-sqlalchemy 文档等等。不幸的是,我找不到任何多对多 sqlalchemy 关系的完整示例。
我有一个使用 flask-appbuilder 的 python Flask 应用程序(它依赖于 flask-sqlalchemy)。我的 app/model.py 文件有这个:
field_feature_association = Table('field_feature_association',Base.metadata,
Column('field_id', Integer, ForeignKey('field.id')),
Column('feature_id',Integer, ForeignKey('feature.id')),
schema="main"
)
class field(Model):
__tablename__ = 'field'
id = Column(Integer, primary_key=True)
name = Column(String(70), nullable=False)
database_type = Column(String(70)) #varchar(255), text, int
joinable_to = Column(Text())
notes = Column(Text()) #don't use this for X
table_id = Column(Integer, ForeignKey('table.id'))
table = relationship("table")
features = relationship("feature",
secondary = field_feature_association,
backref = backref('fields'),
)
def __repr__(self):
return self.name
class feature(Model):
__tablename__ = 'feature'
id = Column(Integer, primary_key=True)
name = Column(String(70), unique = True, nullable=False)
field_id = Column(Integer, ForeignKey('field.id'))
#field = relationship("field")
def __repr__(self):
return self.name
它正在生成此错误:
sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'field_feature_association.feature_id' could not find table 'feature' with which to generate a foreign key to target column 'id'
关于如何修复此错误的想法?
【问题讨论】:
标签: python-2.7 flask flask-sqlalchemy