SAWarning: relationship 'Capital.country' will copy column country.id to column
capital.country_id, which conflicts with relationship(s): 'Country.capital'
(copies country.id to capital.country_id). If this is not the intention,
consider if these relationships should be linked with back_populates,
or if viewonly=True should be applied to one or more if they are read-only.
For the less common case that foreign key constraints are partially
overlapping, the orm.foreign() annotation can be used to isolate the columns
that should be written towards. To silence this warning,
add the parameter 'overlaps="capital"' to the 'Capital.country' relationship.
(Background on this error at: https://sqlalche.me/e/14/qzyx)
后面设置了back_populates参数后,问题解决.
看报错信息,说是冲突,设置back_populates参数就可以,但是不是很明白,想知道一下详细原因.
以下是部分主要代码:
class Country(db.Model):
id = db.Column(db.Integer,primary_key=True)
name = db.Column(db.String(30),unique=True)
capital = db.relationship('Capital',uselist=False)
def __repr__(self):
return '<Country %r>' % self.name
class Capital(db.Model):
id = db.Column(db.Integer,primary_key=True)
name = db.Column(db.String(30),unique=True)
country_id = db.Column(db.Integer,db.ForeignKey('country.id'))
country = db.relationship('Country')
def __repr__(self):
return '<Capital %r>' % self.name