【问题标题】:Does anyone know how to fix "sqlalchemy.exc.AmbiguousForeignKeysError"?有谁知道如何修复“sqlalchemy.exc.AmbiguousForeignKeysError”?
【发布时间】:2020-09-13 18:12:31
【问题描述】:

sqlalchemy.exc.AmbiguousForeignKeysError:无法确定关系 Location.changes_in_location 上父/子表之间的连接条件 - 有多个外键路径链接表。指定 'foreign_keys' 参数,提供应计为包含对父表的外键引用的列的列表。

以前有人问过这个错误,但原因似乎不同。我正在尝试跟踪仓库之间的产品移动。这是我的模型文件的代码:

from inventory import db
from datetime import datetime

class Product(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), unique=True, nullable=False)
    product_movements = db.relationship('Movement', backref='item', lazy=True)

    def __repr__(self):
        return f"{self.name} added."

class Location(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), unique=True, nullable=False)
    changes_in_location = db.relationship('Movement', backref='location', lazy=True)

    def __repr__(self):
        return f"{self.location} added."

class Movement(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    product_id = db.Column(db.Integer, db.ForeignKey('product.id'))
    product = db.Column(db.String(50), nullable=False)
    from_location_id = db.Column(db.Integer, db.ForeignKey('location.id'))
    from_location = db.Column(db.String(50))
    to_location_id = db.Column(db.Integer, db.ForeignKey('location.id'))
    to_location = db.Column(db.String(50))
    quantity = db.Column(db.Integer, nullable=False)
    timestamp = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)

    def __repr__(self):
        return f"{self.quantity} units of {self.product} moved from {self.from_location} to {self.to_location} at {self.timestamp}."

【问题讨论】:

    标签: python database flask sqlalchemy flask-sqlalchemy


    【解决方案1】:

    您应该在模型中为乘法关系添加关系,并在关系中指定 ForeignKeyprimaryjoin

    lass Location(db.Model):
        id = db.Column(db.Integer, primary_key=True)
        name = db.Column(db.String(50), unique=True, nullable=False)
    
        from_locations = db.relationship('Movement', backref='location', lazy=True, primaryjoin='Movement.from_location_id == Location.id')
        to_location = db.relationship('Movement', backref='location', lazy=True, primaryjoin='Movement.to_location_id == Location.id')
    
        def __repr__(self):
            return f"{self.location} added."
    
    class Movement(db.Model):
        id = db.Column(db.Integer, primary_key=True)
        product_id = db.Column(db.Integer, db.ForeignKey('product.id'))
        product = db.Column(db.String(50), nullable=False)
        from_location_id = db.Column(db.Integer, db.ForeignKey('location.id'))
        from_location = db.Column(db.String(50))
        to_location_id = db.Column(db.Integer, db.ForeignKey('location.id'))
        to_location = db.Column(db.String(50))
        quantity = db.Column(db.Integer, nullable=False)
        timestamp = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    
        from_location = db.relationship('Location', backref='from_locations', lazy=True, foreign_keys=[from_location_id])
        to_location = db.relationship('Location', backref='to_locations', lazy=True, foreign_keys=[to_location_id])
    
        def __repr__(self):
            return f"{self.quantity} units of {self.product} moved from {self.from_location} to {self.to_location} at {self.timestamp}."
    

    【讨论】:

    • 嘿,谢谢。您的解决方案最初不起作用,但是在我更改了 Location 类中的 backref 值之后它就起作用了(在此都读取“位置”)。
    • 好的。我运行此代码并且没有错误。但如果您有问题,我很乐意回答并提供帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多