【问题标题】:Is it possible to have SQL Alchemy database models point to attributes of a different model, so that when one model's data changes, so does the other?是否可以让 SQL Alchemy 数据库模型指向不同模型的属性,这样当一个模型的数据发生变化时,另一个模型的数据也会发生变化?
【发布时间】:2021-11-02 04:17:50
【问题描述】:

我正在制作一个非常简单的仓库管理系统,我希望用户能够为商品创建模板。该模板将显示在一个列表中,然后可以单独用于创建项目的实例,该实例还将获得数量和仓库属性。

目标是,如果其中一个商品模板被修改以指定不同的尺寸或价格,则实际商品实例的尺寸或价格属性也会发生变化。

这是我的代码,以帮助您想象我正在尝试做的事情。我不确定这是否可行,或者我是否应该考虑其他解决方案。这是我第一次使用 Flask SQLAlchemy。

class ItemTemplate(db.model):

    """This template will simply store the information related to an item type.
    Individual items that will be associated with the warehouse they're stored in
    will inherit this information from the item templates."""

    _id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(15), unique=True, nullable=False)
    price = db.column(db.Float, nullable=False)
    cost = db.column(db.Float, nullable=False)
    size = db.column(db.Integer, nullable=False)
    lowThreshold = db.column(db.Integer, nullable=False)


# Actual items
class Item(db.model):

    """This template will be used to represent the actual items that are associated with a warehouse."""

    _id = db.Column(db.Integer, primary_key=True)
    quantity = db.Column(db.Integer, primary_key=True)

    """Here I want the Item attributes to be able to just point to attributes from the ItemTemplate class.
    
    ItemTemplate(name='tape') <--- will be a template with the information for tape.
    Item(name='tape') <--- will be an actually instance of tape that should inherit all the attributes from the tape template.
    
    I want these attributes to be like pointers so that if the tape template has its name changed, for instance, to
    'scotch tape', all the Item instances that point to the tape template will have their names changed."""


# Warehouse
class Warehouse(db.model):

    _id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(15), unique=True, nullable=False)
    capacity = db.Column(db.column(db.Integer, nullable=False))
    items = db.relationship("Item", backref="warehouse", lazy=True)```

【问题讨论】:

    标签: python database flask-sqlalchemy


    【解决方案1】:

    据我了解,您只需声明 ItemTemplateItem 之间的一对多关系,即一个模板将用于多个项目。

    定义模型

    试着像这样声明他们的关系

    class ItemTemplate(db.model):
        _id = db.Column(db.Integer, primary_key=True)
        ... # Other attribute
    
        instances = db.relationship('item', backref='item_template', lazy=True)
    
    
    class Item(db.model):
        _id = db.Column(db.Integer, primary_key=True)
        quantity = db.Column(db.Integer, primary_key=True)
    
        item_template_id = db.Column(db.Integer, db.ForeignKey('item_template._id'), nullable=False)
    
    

    有关关系的更多信息的文档: https://flask-sqlalchemy.palletsprojects.com/en/2.x/models/#one-to-many-relationships

    查询

    下次查询时,只需连接两个表即可获得ItemTemplate.name

    items_qr = db.session.query(Item, ItemTemplate.name).join(ItemTemplate)
    
    for item, item_name in items_qr:
        print(item.id, item_name)
    

    query.join() 的 SQLAlchemy 文档:https://docs.sqlalchemy.org/en/14/orm/query.html#sqlalchemy.orm.Query.join

    一些相关的 SO 问题可能会有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-25
      • 1970-01-01
      • 2019-01-06
      • 2018-04-11
      • 1970-01-01
      相关资源
      最近更新 更多