[英]Flask-SQLAlchemy how to store list of ForeignKey Objects
I am creating an application that will gather requirements for a number of different 'Types' of items, and can't figure out how to structure the Flask-SQLAlchemy ORM Model relationship(s).我正在创建一个应用程序,它将收集许多不同“类型”项目的要求,但无法弄清楚如何构建 Flask-SQLAlchemy ORM Model 关系。
I have the following Classes:我有以下课程:
ItemSize - A 'Type', think of liek a T-Shirt size. 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 - A collection of a particular ItemSize, stores relationship to ItemSize and a Count. ItemSet - 特定 ItemSize 的集合,存储与 ItemSize 和 Count 的关系。 (Eg. ItemSize: 'A', Count: 2)
(例如,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 - A collection of many ItemSets.要求 - 许多 ItemSet 的集合。 (Eg. ItemSets [1, 2, 3, 4, 5])
(例如,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')
The above gives me the following error when trying to create any objects:以上在尝试创建任何对象时给了我以下错误:
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.
Does anyone know how to achieve this kind of relationship?有谁知道如何实现这种关系?
Specification.requirements -> [Array of ItemSets]
Any pointers very much appreciated.任何指针都非常感谢。
You need to modify these 2 classes:您需要修改这两个类:
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 = relationship("ItemSet", back_populates="spec")
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)
spec_id = Column(db.Integer, ForeignKey('spec.id'))
spec = relationship("Specification", back_populates="requirements")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.