简体   繁体   中英

Does anyone know how to fix “sqlalchemy.exc.AmbiguousForeignKeysError”?

sqlalchemy.exc.AmbiguousForeignKeysError: Could not determine join condition between parent/child tables on relationship Location.changes_in_location - there are multiple foreign key paths linking the tables. Specify the 'foreign_keys' argument, providing a list of those columns which should be counted as containing a foreign key reference to the parent table.

Someone else had asked about this error before, but the cause seems to have been different. I'm trying to track product movements between warehouses. This is the code for my models file:

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}."

You should be adding the relationship in models for multiply relation and specify the ForeignKey and primaryjoin in the relationship.

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}."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM