【发布时间】: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