登录
首页 >  Golang >  Go问答

使用GORM模型在Golang中基于外键关系查询记录

来源:stackoverflow

时间:2024-03-14 16:27:25 409浏览 收藏

编程并不是一个机械性的工作,而是需要有思考,有创新的工作,语法是固定的,但解决问题的思路则是依靠人的思维,这就需要我们坚持学习和更新自己的知识。今天golang学习网就整理分享《使用GORM模型在Golang中基于外键关系查询记录》,文章讲解的知识点主要包括,如果你对Golang方面的知识点感兴趣,就不要错过golang学习网,在这可以对大家的知识积累有所帮助,助力开发能力的提升。

问题内容

我正在使用 golang (gorm) + postgres。我正在尝试对一种业务情况进行建模,其中卖方将商品出售给买方,每个买方都创建一个订单交易。

我有一个 order gorm 模型,以及一个 buyer 和 seller gorm 模型。买家和卖家已在数据库中创建行。

一个买家有很多订单。

一个卖家有很多订单。

为了绘制出这种关系,我相信我只需创建相应的 buyer/seller 结构(标准 gorm 模型),然后创建一个 order 结构,如下所示:

type order struct {
    id        int64       `json:"id"gorm:"primary_key;auto_increment:true"`
    buyer     *buyer      `json:"buyer"`
    seller    *seller     `json:"seller"`
    // ... other data ...
}

我假设上面的内容自动创建了关系,但我不完全确定。我可以使用此函数创建一个订单,并且返回结果良好:

func createorder(buyer *entity.buyer, seller *entity.seller) (*entity.order, error) {
    order := &entity.order{
        user:   buyer,
        sitter: seller,
        // ... other data ...
    }
    db.table("orders").create(order)
    return order
}

如果我进入 postgres cli,table 订单; 不会显示买家或卖家列。我希望有一列他们的 id。所以这就是为什么我不确定这是否有效。 这本身肯定是一个问题。

无论如何,我真正想要做的是能够检查买家/卖家当前是否存在任何订单。但我真的不认为用 gorm 查询来做到这一点。我想象在 sql 中它会是这样的:

func FindOrder(buyer *entity.Buyer, seller *entity.Seller) {
     db.Raw(`GET order FROM orders WHERE buyer = ?, seller = ?`, buyer, seller)
     // OR this ???
     db.Table("orders").First(buyer, buyer).First(seller, seller)
}

但我不知道有任何 gorm 辅助函数实际上可以做到这一点。我还希望这能够高效,因为买家和卖家都有自己的 id 主键。

如何像上面的示例一样根据买方/卖方查找订单?

作为替代方案,我正在考虑添加(买家 id + 卖家 id)来制作自定义订单 id primary_key。但这看起来很老套,因为我觉得关系的全部意义在于我不必做这样的事情。


正确答案


如果您需要在订单表中查看卖家 id 和买家 id,请在订单结构中包含两个字段,您也可以使用 foreignkey 标签来填充这些字段(默认情况下,它们填充有主字段)关联表记录的 id,您可以使用 references 标签(如提到的 here 来更改)。

type order struct {
    id int64 `json:"id" gorm:"primarykey;autoincrement:true"`
    buyerid int64 `json:"buyer_id" gorm:"index"`
    sellerid int64 `json:"seller_id" gorm:"index"`
    buyer *buyer `json:"buyer" gorm:"foreignkey:buyerid"`
    seller *seller  `json:"seller" gorm:"foreignkey:sellerid"`
}

type buyer struct {
    id int64    `json:"id" gorm:"primarykey;autoincrement:true"`
    name string `json:"name"`
}

type seller struct {
    id int64    `json:"id" gorm:"primarykey;autoincrement:true"`
    name string  `json:"name"`
}

至于查找给定买家和卖家的订单的功能,您可以使用类似的功能,

func findorders(db *gorm.db, buyerid int,sellerid int)[]order{
    orders := make([]order,0)
    db.where("buyer_id=? and seller_id=?",buyerid,sellerid).find(&order{}).scan(&orders)
    return orders
}

相反,如果您需要查找给定买家或卖家的订单,

func findOrder(db *gorm.DB, buyerID int,sellerID int)[]Order{
    orders := make([]Order,0)
    db.Where("buyer_id=? OR seller_id=?",buyerID,sellerID).Find(&Order{}).Distinct().Scan(&orders)
    return orders
}

index 标记满足 orders 表的索引要求。

使用简单的原始查询构建器进行大型查询 https://perfilovstanislav.github.io/go-raw-postgresql-builder/#simple-example

理论要掌握,实操不能落!以上关于《使用GORM模型在Golang中基于外键关系查询记录》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>