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).
I have the following Classes:
ItemSize - A 'Type', think of liek a T-Shirt size.
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. (Eg. ItemSize: 'A', Count: 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. (Eg. 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")
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.