这是我参与「第三届青训营 -后端场」笔记创作活动的的第2篇笔记。
- 课前预习 GORM语法
1.1 foreign key
1.2
Auto migration will only create tables, missing columns and indexes and won't remove stuff.
The first line of the func create three tables having the field the corresponding struct have.
db.first(...) pass in the pointer the variable that you want to unmarshall into, to determine the table you want to query, and it will only take the first one find
Also, name=?, general becomes name= 'general'
Similar commands in query tag on the website
1.3
db,err = gorm.Open(...)
...
defer db.close()
connect to database
db.LogMode(true)
enable logger
db.Model(&a).Related(&b)
select * from b where user_id = a.user_id
find every b belongs to a
related corresponding to belong to
1.4 error handling
- 课堂内容 2.1 基本用法
note:
因为close可能出错
拿error的方式
Create Record
2.2 关联介绍
Preload/Join
级联删除
2.3 插件
2.4 ConnPool
2.5 interpolateParam=false 提高效率
2.6 dialector
2.7 数列数据化
2.8 批量操作
加速操作
2.9 代码复用
3.0 sharding
4.0 Logger/Trace
5.0 安全问题