【问题标题】:Can't connect two element in one table. sqlalchemy无法在一张表中连接两个元素。 sqlalchemy
【发布时间】: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


    【解决方案1】:

    写一个没有关系的联结表。只是一个带有项目 ID 的表格

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-04
      • 2020-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多