首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >一对多插入

一对多插入
EN

Stack Overflow用户
提问于 2022-08-01 03:18:31
回答 2查看 68关注 0票数 1

我有两个模型:OrderOrderItem。我需要从同一请求中插入订单和项目,例如:

代码语言:javascript
运行
复制
{
    "user_id": "1",
    "total_price": "200",
    "items": [
        {
            "product_id": 1,
            "quantity": 10
        },
        {
            "product_id": 2,
            "quantity": 5
        },
        {
            "product_id": 3,
            "quantity":3
        }
    ]
}

这是Order和OrderItem模型

代码语言:javascript
运行
复制
=========================== Order ===========================
type Order struct {
    ID          int       `boil:"id" json:"id" toml:"id" yaml:"id"`
    OrderNumber string    `boil:"order_number" json:"order_number" toml:"order_number" yaml:"order_number"`
    OrderDate   time.Time `boil:"order_date" json:"order_date" toml:"order_date" yaml:"order_date"`
    Status      string    `boil:"status" json:"status" toml:"status" yaml:"status"`
    Note        string    `boil:"note" json:"note" toml:"note" yaml:"note"`
    UserID      int       `boil:"user_id" json:"user_id" toml:"user_id" yaml:"user_id"`
    CreatedAt   time.Time `boil:"created_at" json:"created_at" toml:"created_at" yaml:"created_at"`
    UpdatedAt   time.Time `boil:"updated_at" json:"updated_at" toml:"updated_at" yaml:"updated_at"`

    R *orderR `boil:"-" json:"-" toml:"-" yaml:"-"`
    L orderL  `boil:"-" json:"-" toml:"-" yaml:"-"`
}

=========================== OrderItem ===========================
type OrderItem struct {
    ID           int       `boil:"id" json:"id" toml:"id" yaml:"id"`
    OrderID      int       `boil:"order_id" json:"order_id" toml:"order_id" yaml:"order_id"`
    ProductID    int       `boil:"product_id" json:"product_id" toml:"product_id" yaml:"product_id"`
    ProductPrice float64   `boil:"product_price" json:"product_price" toml:"product_price" yaml:"product_price"`
    ProductName  string    `boil:"product_name" json:"product_name" toml:"product_name" yaml:"product_name"`
    Quantity     int       `boil:"quantity" json:"quantity" toml:"quantity" yaml:"quantity"`
    Discount     float64   `boil:"discount" json:"discount" toml:"discount" yaml:"discount"`
    Note         string    `boil:"note" json:"note" toml:"note" yaml:"note"`
    CreatedAt    time.Time `boil:"created_at" json:"created_at" toml:"created_at" yaml:"created_at"`
    UpdatedAt    time.Time `boil:"updated_at" json:"updated_at" toml:"updated_at" yaml:"updated_at"`

    R *orderItemR `boil:"-" json:"-" toml:"-" yaml:"-"`
    L orderItemL  `boil:"-" json:"-" toml:"-" yaml:"-"`
}

人们通常做什么?有什么方法可以快速解决这个问题吗?

EN

回答 2

Stack Overflow用户

发布于 2022-08-01 07:23:21

我认为您不需要这样做,您可以在不同的请求中插入订单模型和orderItems模型。此解决方案提供了比客户端更多的选项。如果您需要在订单中更新或添加新的订单项,您还需要这个API来解决这个问题。

票数 2
EN

Stack Overflow用户

发布于 2022-08-01 03:37:26

创建一些模型,如

代码语言:javascript
运行
复制
type OrderItemInput struct {
    ProductId int `json:"product_id"`
    Quantity  int `json:"quantity"`
}

type OrderInsertInput struct {
    UserID      int        `json:"user_id"`
    TotalPrice  float64    `json:"total_price"`
    Items []OrderItemInput `json:"items"`
}

按字段UserIdTotalPrice of OrderInsertInput创建新的顺序。

当存在OrderID时,我们将使用每个OrderItemInputOrderID创建OrderItem

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73188526

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档