当前位置:网站首页>关于gorm的preload方法笔记说明
关于gorm的preload方法笔记说明
2022-06-11 20:55:00 【dorlolo】
gorm的preload预加载笔记
假设,有三张表的层级关系为 Audit–>User -->homePage (具体表结构请查看末尾的大标题)
像下面的方法,是取不到auditData.Auditor中的值的
var auditData Audit
global.GVA_DB.Model(&auditData).Where("id = 1").First(&auditData)
常规预加载
如果要拿到这个Auditor对象
举例我要查找Audit表中id为1的数据,并且要拿到Auditor字段中的数据
var auditData Audit
global.GVA_DB.Model(&auditData).Preload("Auditor").Where("id = 1").First(&auditData)
多级预加载
举例我要查找Audit表中id为1的数据,除了要拿到Auditor中的数据外,Auditor中HomePage的数据也要拿到。
var auditData Audit
global.GVA_DB.Model(&auditData).Preload("Auditor.HomePage").Where("id = 1").First(&auditData)
preload的原理
其实很简单,你要preload user表的数据,gorm就提前把这张表全部取出来,然后再执行First或Find这样的方法去查询主数据。最后把两种数据一一对应起来。
示例中的数据表结构体说明
假设有以下三张表的结构体
homePage 主页表结构体
type HomePage struct { global.GVA_MODEL UserId uint `json:"userId" gorm:"column:user_id;comment:'用户id'"` Desc string `json:"desc" form:"desc" gorm:"column:desc;type:varchar(500);comment:主页描述 "` Phone string `json:"phone" form:"phone" gorm:"column:phone;comment:联系电话"` Email string `json:"email" form:"email" gorm:"column:email;comment:邮箱"` } func (HomePage) TableName() string { return "home_page" }这张表主要特点是有一个
UserId,用来将单挑数据绑定到下面的User表中的某条数据。User 用户表结构体
type User struct { global.GVA_MODEL Username string `json:"userName" form:"userName" gorm:"column:user_name;comment:用户登录名"` HomePage HomePage `json:"homePage" gorm:"foreignkey:UserId"` } func (User) TableName() string { return "user" }这里用到了gorm的Belongs To,外键关联了HomePage表,这个HomePage在实际的User表中是不存在的。
Audit审批表结构体
type Audit struct { global.GVA_MODEL //提交人 SubmitterId uint `json:"submitterId" form:"submitterId" gorm:"column:submitter_id;comment: 申请单提交用户的id"` Submitter User `gorm:"foreignkey:SubmitterId"` //审批人 AuditorId uint `json:"auditorId" form:"auditorId" gorm:"column:auditor_id;comment:审批人id"` Auditor User `gorm:"foreignkey:AuditorId"` AuditStatus uint `json:"auditStatus" form:"auditStatus" gorm:"column:audit_status;comment:审批状态:0待审核 1未通过 2已通过"` } func (Audit) TableName() string { return "audit" }里面的Submitter和Auditor通过gorm的Has One关联到了User表。
边栏推荐
- BCC tool tool usage
- Serval and Rooted Tree(CF1153D)-DP
- 周刊02|不瞒你说,我其实是MIT的学生
- MySQL installation free configuration tutorial under Windows mysql-5.6.51-winx64 Zip version
- [Monday commuter radio station] cron expression. It's enough to read this article
- Work assessment of spectral analysis of Jilin University in March of the 22nd spring -00079
- UI automated interview questions
- 28. JS implementation mechanism
- Frequency domain filter
- Current situation and future development trend of global and Chinese cogeneration system market from 2022 to 2028
猜你喜欢
![[file upload vulnerability 04] server side mime detection and bypass experiment (based on upload-labs-2 shooting range)](/img/b8/521ca4bb8931afab9a3a4d0b015125.jpg)
[file upload vulnerability 04] server side mime detection and bypass experiment (based on upload-labs-2 shooting range)

Frequency domain filter

9 r remove missing values
![[nk] 牛客练习赛100 C 小红的删数字](/img/f1/a99600e1800c087aceb60a559dee39.png)
[nk] 牛客练习赛100 C 小红的删数字

Weekly 02 | to tell you the truth, I am actually a student of MIT

php pcntl_ Fork create multiple child process resolution

Cuckoo Hash

File upload vulnerability - simple exploitation 2 (Mozhe college shooting range)
![[game theory complete information static game] strategic game](/img/d2/743e8d14e4fb27cbe883d1df1bca27.jpg)
[game theory complete information static game] strategic game

Solution to the problem of PHP strtotime obtaining natural monthly error
随机推荐
unity package manager starting server stuck(Unity启动卡在starting server,然后报错)
Log in with password and exit with error for three times.
[data visualization] use Apache superset to visualize Clickhouse data
[Unity插件]着色器关键字分析工具ShaderControl
Space transcriptome experiment | what factors will affect the quality of space transcriptome sequencing during the preparation of clinical tissue samples?
Which Bluetooth headset is better within 500? Inventory of gifts for girls' Day
[nk] deleted number of 100 C Xiaohong in Niuke practice match
Redis第四话 -- redis高性能原理(多路复用)和高可用分析(备份、主从)
Application scenario: wide application of Poe network card in NDI technology for live broadcast program production
应用场景:现场直播节目制作NDI技术中PoE网卡的广泛应用
Pyqt5 technical part - cause of the problem that setting the top of the window does not take effect. Setwindowflags() does not take effect after setting the parameters. Solution
正则校验匹配[0-100]、[0-1000]之间的正整数或小数点位数限制
Summary of C language programming knowledge points 01
[unity plug-in] shader keyword analysis tool shadercontrol
Toolbar替换ActionBar后Title不显示
File upload vulnerability - simple exploitation 2 (Mozhe college shooting range)
8 r subset
Systematically study the recommendation system from a global perspective to improve competitiveness in actual combat (Chapter 8)
Windows icon display exception resolution. The desktop icon is abnormal, the start menu icon is abnormal, and the taskbar icon is abnormal. Icon cache location.
Vectordrawable error