在SQLAlchemy Flask中如何创建一对多关系

huangapple go评论45阅读模式
英文:

How to create one-to-many relationships in SQLAlchemy Flask

问题

我是Python Flask的初学者,想请教您如何在Flask SQLAlchemy数据库中创建一对多关系?

我的数据库类:

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String, unique=True, nullable=False)
    email = db.Column(db.String)

class Order(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    ProductName = db.Column(db.String, unique=True, nullable=False)
    price = db.Column(db.String)

请您提供如何在这些表之间创建关系的解释。

英文:

I am a beginner in Python Flask and would like to ask for your help. How do I create a one-to-many relationship in a Flask SQLAlchemy database?

My db class:

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String, unique=True, nullable=False)
    email = db.Column(db.String)

class Order(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    ProductName= db.Column(db.String, unique=True, nullable=False)
    price= db.Column(db.String)

Could you please provide an explanation of how to create a relationship between the tables.

答案1

得分: 3

要在用户(User)和订单(Order)模型之间创建一对多关系,您可以使用外键(ForeignKey)关系。具体来说,您可以向订单模型添加一个外键,该外键引用用户模型的主键。

在用户模型中,我们已经添加了一个新的属性 orders,它表示与订单模型的一对多关系。我们使用 relationship 函数来指定此关系,该函数接受几个参数:

  • 'Order':相关模型的名称
  • backref='user':在订单模型上创建一个新属性 user,该属性引用用户模型
  • lazy=True:指定SQLAlchemy应在需要时加载关系(即使用单独的SQL查询)

在订单模型中,我们添加了一个新的 user_id 属性,它表示与用户模型的外键关系。我们使用 ForeignKey 函数来指定此关系,该函数接受相关表格和列的名称('user.id')作为参数。我们还使用 nullable=False 指定此列不能为空。

英文:

To create a one-to-many relationship between the User and Order models, you can use a ForeignKey relationship.
Specifically, you can add a foreign key to the Order model that references the primary key of the User model.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String, unique=True, nullable=False)
    email = db.Column(db.String)
    orders = db.relationship(&#39;Order&#39;, backref=&#39;user&#39;, lazy=True)

class Order(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    product_name = db.Column(db.String, unique=True, nullable=False)
    price = db.Column(db.String)
    user_id = db.Column(db.Integer, db.ForeignKey(&#39;user.id&#39;), nullable=False)

<!-- end snippet -->

In the User model, we have added a new orders attribute that represents a one-to-many relationship with the Order model. We specify this relationship using the relationship function, which takes several arguments:

  • 'Order': the name of the related model

  • backref='user': creates a new attribute on the Order model called user that refers back to the User model

  • lazy=True: specifies that SQLAlchemy should load the relationship as
    needed (i.e., using a separate SQL query)

In the Order model, we have added a new user_id attribute that represents the foreign key relationship with the User model.

We specify this relationship using the ForeignKey function, which takes the name of the related table and column ('user.id') as an argument. We also specify that this column cannot be null using nullable=False.

huangapple
  • 本文由 发表于 2023年4月13日 21:27:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定