簡體   English   中英

有誰知道如何修復“sqlalchemy.exc.AmbiguousForeignKeysError”?

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

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

您應該在乘法關系模型中添加關系,並在關系中指定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}."

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM