【发布时间】:2021-08-02 17:46:12
【问题描述】:
我正在创建一个应用程序,它将收集许多不同“类型”项目的需求,但不知道如何构建 Flask-SQLAlchemy ORM 模型关系。
我有以下课程:
ItemSize - 一个“类型”,类似于 T 恤尺寸。
class ItemSize(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
date_created = db.Column(db.Date(), default=datetime.now())
height = db.Column(db.Integer, nullable=True)
depth = db.Column(db.Integer, nullable=True)
width = db.Column(db.Integer, nullable=True)
ItemSet - 特定 ItemSize 的集合,存储与 ItemSize 和 Count 的关系。 (例如,ItemSize:'A',计数:2)
class ItemSet(db.Model):
id = db.Column(db.Integer, primary_key=True)
size = db.Column(db.Integer, db.ForeignKey('itemsizes.id'), nullable=True)
count = db.Column(db.Integer, nullable=False)
Requirements - 许多 ItemSet 的集合。 (例如,ItemSets [1, 2, 3, 4, 5])
class Specification(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
comments = db.Column(db.String(255), nullable=True)
# THIS IS THE BIT I AM UNSURE OF
# Need this to a collection of Many ItemSets
# Will the below work?
requirements = db.relationship('ItemSet', uselist=True, backref='itemsets')
在尝试创建任何对象时,上面给出了以下错误:
sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'mapped class Specification->specifications'. Original exception was: Could not determine join condition between parent/child tables on relationship Specification.requirements - there are no foreign keys linking these tables. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.
有谁知道如何建立这种关系?
Specification.requirements -> [Array of ItemSets]
非常感谢任何指针。
【问题讨论】:
标签: python flask sqlalchemy orm flask-sqlalchemy