【问题标题】:Flask-SQLAlchemy how to store list of ForeignKey ObjectsFlask-SQLAlchemy 如何存储 ForeignKey 对象列表
【发布时间】: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


    【解决方案1】:

    你需要修改这2个类:

    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")
    

    【讨论】:

      猜你喜欢
      • 2020-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-25
      • 2020-01-30
      • 1970-01-01
      • 2021-11-29
      • 2022-01-20
      相关资源
      最近更新 更多