我认为我正确定义了我想要将社区与他们的议程联系起来的数据模型但是我遇到以下错误。 你能给点提示吗?
NoForeignKeysError:无法确定关系CSCommunity.agenda上的父/子表之间的连接条件 - 没有链接这些表的外键。确保引用列与ForeignKey或ForeignKeyConstraint相关联,或指定' primaryjoin'表达
以下代码。
class Agenda(db.Model):
__tablename__ = 'communityagenda'
id = db.Column(db.Integer, primary_key=True)
csc = db.Column(db.Integer, db.ForeignKey('scorecards.id'))
cscommunity = db.Column(db.Integer, db.ForeignKey('cscommunities.id'))
meeting_date = db.Column(db.DateTime)
notes = db.Column(db.Text)
class CSCommunity(db.Model):
__tablename = 'cscommunities'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(150))
key_leaders = db.Column(db.Text)
subgroup = db.Column(db.Text)
#---------------------------
region = db.Column(db.Integer, db.ForeignKey('regions.id'))
district = db.Column(db.Integer, db.ForeignKey('districts.id'))
subdistrict = db.Column(db.Integer, db.ForeignKey('subdistricts.id'))
village = db.Column(db.Integer, db.ForeignKey('villages.id'))
agenda = db.relationship('Agenda', backref='cscommunity', cascade='all, delete-orphan', lazy='dynamic')
faclilitators = db.relationship('Facilitators', backref='cscommunities', cascade='all, delete-orphan', lazy='dynamic')
答案 0 :(得分:0)
此代码存在以下几个问题(如果我们跳过您的代码段中未定义地区,区域,子区域,村庄和辅导员的事实):
__tablename = 'cscommunities'
应为__tablename__ = 'cscommunities'
(请注意,您错过了第二个' __')
您创建的backref='cscommunity'
(关系)与Agenda.cscommunity
(列)冲突。重命名你的背景。
我在修复后都没有出错(SQLAlchemy == 1.0.4)