【发布时间】:2021-03-08 09:10:00
【问题描述】:
试图在一个表内建立m2m连接,但出现错误:
sqlalchemy.exc.AmbiguousForeignKeysError: Could not determine join condition between parent/child tables on relationship Products.compatibilities - there are multiple foreign key paths linking the tables via secondary table 'сompatibilites'. Specify the 'foreign_keys' argument, providing a list of those columns which should be counted as containing a foreign key reference from the secondary table to each of the parent and child tables.
我的代码:
сompatibilites = db.Table('сompatibilites', db.Model.metadata,
db.Column('left_id', db.Integer, db.ForeignKey('products.id')),
db.Column('right_id', db.Integer, db.ForeignKey('products.id'))
)
class Products(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255))
type = db.Column(db.String(63))
text = db.Column(db.String(1024))
specs = db.relationship('Specs', backref='product', lazy='dynamic')
slides = db.relationship('ProductSlides', backref='product', lazy='dynamic')
img = db.Column(db.String(6), unique=True)
price = db.Column(db.Integer)
compatibilities = db.relationship('Products', secondary=сompatibilites, backref=db.backref('compatibilities', lazy='dynamic'))
【问题讨论】:
标签: python sqlalchemy many-to-many flask-sqlalchemy