在 Gorm 中实现与值数组的关系
在 Gorm 中,实现与值数组的关系是一项非常有用的功能。Gorm 是一个流行的 Go 语言 ORM(对象关系映射)库,它允许开发者使用 Go 语言来操作数据库。与传统的关系型数据库不同,值数组是一种特殊的数据结构,它允许将多个值作为一个整体存储在数据库中的某个字段中。这对于存储一些复杂的数据结构或者提高查询效率都非常有帮助。在本文中,我将向大家介绍如何在 Gorm 中实现与值数组的关系,以及如何使用它来提升开发效率。
问题内容
我正在尝试使用 go 和 gorm 实现发票应用程序的模型。我已经定义了发票结构,并希望包含来自单独结构的发票行项目。
type invoice struct { base companyid string `gorm:"not null"` company company invoiceno string `gorm:"not null"` currency string `gorm:"not null;default:'gbp'"` total float64 `gorm:"not null"` terms int `gorm:"not null;default:30"` issueddate time.time `gorm:"not null"` duedate time.time `gorm:"not null"` paid bool `gorm:"not null"` lineitems []lineitem `gorm:"foreignkey:invoiceid"` void bool `gorm:"default:false"` }
这是我的 lineitem 结构。
type lineitem struct { base service string `gorm:"not null"` description string `gorm:"not null;"` amount float64 count int unitbase string `gorm:"not null;default:'days'"` // days or hours total float64 `gorm:"not null"` }
当我尝试构建应用程序时,出现以下错误。
... got error invalid field found for struct github.com/repo/API/database/models.Invoice's field LineItems: define a valid foreign key for relations or implement the Valuer/Scanner interface
我的想法是,我可以定义一个有限的设置订单项,可以从固定费率和说明中进行选择,以限制重复。
我不确定我的处理方式是否正确。
所以我的问题是,是否可以通过这种方式包含关系模型中的项目数组?
解决方法
根据您的列名称,我可以想到三种方法来实现此目的:
使用默认语法(无
gorm:foreignkey
)type invoice struct { id //this is the primary key, may also be in your base model lineitems []lineitem ..other fields } type lineitem struct { id //primary key of lineitem invoiceid //automatically this will be your foreign key ..other fields }
登录后复制指定自定义外键(假设第二个结构具有不同的列名称,您希望将其作为外键)
type invoice struct { id //this is the primary key, may also be in your base model lineitems []lineitem `gorm:"foreignkey:parentid"` ..other fields } type lineitem struct { id //primary key of lineitem parentid //this is the custom column referencing invoice.id ..other fields }
登录后复制或者两个结构体具有不同的列名称
type Invoice struct { ID //this is the primary key, may also be in your base model InvoiceNum Lineitems []LineItem `gorm:"foreignKey:ParentNum;references:InvoiceNum"` ..other fields } type LineItem struct { ID //primary key of LineItem ParentNum //this is now referencing Invoice.InvoiceNum ..other fields }
登录后复制以上是在 Gorm 中实现与值数组的关系的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

本文解释了GO的软件包导入机制:命名imports(例如导入“ fmt”)和空白导入(例如导入_ fmt; fmt;)。 命名导入使包装内容可访问,而空白导入仅执行t

本文解释了Beego的NewFlash()函数,用于Web应用程序中的页间数据传输。 它专注于使用newflash()在控制器之间显示临时消息(成功,错误,警告),并利用会话机制。 Lima

本文详细介绍了MySQL查询结果的有效转换为GO结构切片。 它强调使用数据库/SQL的扫描方法来最佳性能,避免手动解析。 使用DB标签和Robus的结构现场映射的最佳实践

本文演示了创建模拟和存根进行单元测试。 它强调使用接口,提供模拟实现的示例,并讨论最佳实践,例如保持模拟集中并使用断言库。 文章

本文探讨了GO的仿制药自定义类型约束。 它详细介绍了界面如何定义通用功能的最低类型要求,从而改善了类型的安全性和代码可重复使用性。 本文还讨论了局限性和最佳实践

本文详细介绍了在GO中详细介绍有效的文件,将OS.WriteFile(适用于小文件)与OS.openfile和缓冲写入(最佳大型文件)进行比较。 它强调了使用延迟并检查特定错误的可靠错误处理。

本文使用跟踪工具探讨了GO应用程序执行流。 它讨论了手册和自动仪器技术,比较诸如Jaeger,Zipkin和Opentelemetry之类的工具,并突出显示有效的数据可视化
