CURD操作

一、创建

1.1 创建记录

1user := User{Name: "Jinzhu", Age: 18, Birthday: time.Now()}
2
3db.NewRecord(user) // => 主键为空返回`true`
4
5db.Create(&user)
6
7db.NewRecord(user) // => 创建`user`后返回`false`

1.2 默认值

你可以通过 tag 定义字段的默认值,比如:

1type Animal struct {
2  ID   int64
3  Name string `gorm:"default:'galeone'"`
4  Age  int64
5}

生成的 SQL 语句会排除没有值或值为 零值 的字段。 将记录插入到数据库后,Gorm会从数据库加载那些字段的值。

1var animal = Animal{Age: 99, Name: ""}
2db.Create(&animal)
3// INSERT INTO animals("age") values('99');
4// SELECT name from animals WHERE ID=111; // 返回主键为 111
5// animal.Name => 'galeone'

注意 所有字段的零值, 比如 0, '', false 或者其它 零值,都不会保存到数据库内,但会使用他们的默认值。 如果你想避免这种情况,可以考虑使用指针或实现 Scanner/Valuer 接口,比如:


 1// 使用指针
 2type User struct {
 3  gorm.Model
 4  Name string
 5  Age  *int `gorm:"default:18"`
 6}
 7
 8// 使用 Scanner/Valuer
 9type User struct {
10  gorm.Model
11  Name string
12  Age  sql.NullInt64 `gorm:"default:18"`  // sql.NullInt64 实现了Scanner/Valuer接口
13}

1.3 在Hooks中设置字段值

如果你想在BeforeCreate hook 中修改字段的值,可以使用scope.SetColumn,例如:

1func (user *User) BeforeCreate(scope *gorm.Scope) error {
2  scope.SetColumn("ID", uuid.New())
3  return nil
4}

1.4 扩展创建选项

1// 为Instert语句添加扩展SQL选项
2db.Set("gorm:insert_option", "ON CONFLICT").Create(&product)
3// INSERT INTO products (name, code) VALUES ("name", "code") ON CONFLICT;

二、查询

2.1 查询

 1// 根据主键查询第一条记录
 2db.First(&user)
 3//// SELECT * FROM users ORDER BY id LIMIT 1;
 4
 5// 随机获取一条记录
 6db.Take(&user)
 7//// SELECT * FROM users LIMIT 1;
 8
 9// 根据主键查询最后一条记录
10db.Last(&user)
11//// SELECT * FROM users ORDER BY id DESC LIMIT 1;
12
13// 查询所有的记录
14db.Find(&users)
15//// SELECT * FROM users;
16
17// 查询指定的某条记录(仅当主键为整型时可用)
18db.First(&user, 10)
19//// SELECT * FROM users WHERE id = 10;

2.2 Where 条件

普通 SQL

 1// Get first matched record
 2db.Where("name = ?", "jinzhu").First(&user)
 3//// SELECT * FROM users WHERE name = 'jinzhu' limit 1;
 4
 5// Get all matched records
 6db.Where("name = ?", "jinzhu").Find(&users)
 7//// SELECT * FROM users WHERE name = 'jinzhu';
 8
 9// <>
10db.Where("name <> ?", "jinzhu").Find(&users)
11//// SELECT * FROM users WHERE name <> 'jinzhu';
12
13// IN
14db.Where("name IN (?)", []string{"jinzhu", "jinzhu 2"}).Find(&users)
15//// SELECT * FROM users WHERE name in ('jinzhu','jinzhu 2');
16
17// LIKE
18db.Where("name LIKE ?", "%jin%").Find(&users)
19//// SELECT * FROM users WHERE name LIKE '%jin%';
20
21// AND
22db.Where("name = ? AND age >= ?", "jinzhu", "22").Find(&users)
23//// SELECT * FROM users WHERE name = 'jinzhu' AND age >= 22;
24
25// Time
26db.Where("updated_at > ?", lastWeek).Find(&users)
27//// SELECT * FROM users WHERE updated_at > '2000-01-01 00:00:00';
28
29// BETWEEN
30db.Where("created_at BETWEEN ? AND ?", lastWeek, today).Find(&users)
31//// SELECT * FROM users WHERE created_at BETWEEN '2000-01-01 00:00:00' AND '2000-01-08 00:00:00';

Struct & Map

 1// Struct
 2db.Where(&User{Name: "jinzhu", Age: 20}).First(&user)
 3//// SELECT * FROM users WHERE name = "jinzhu" AND age = 20 LIMIT 1;
 4
 5// Map
 6db.Where(map[string]interface{}{"name": "jinzhu", "age": 20}).Find(&users)
 7//// SELECT * FROM users WHERE name = "jinzhu" AND age = 20;
 8
 9// 主键的切片
10db.Where([]int64{20, 21, 22}).Find(&users)
11//// SELECT * FROM users WHERE id IN (20, 21, 22);

提示 当通过结构体进行查询时,GORM将会只通过非零值字段查询,这意味着如果你的字段值为0''false 或者其他 零值时,将不会被用于构建查询条件,例如:


1db.Where(&User{Name: "jinzhu", Age: 0}).Find(&users)
2//// SELECT * FROM users WHERE name = "jinzhu";

你可以使用指针或实现 Scanner/Valuer 接口来避免这个问题.

 1// 使用指针
 2type User struct {
 3  gorm.Model
 4  Name string
 5  Age  *int
 6}
 7
 8// 使用 Scanner/Valuer
 9type User struct {
10  gorm.Model
11  Name string
12  Age  sql.NullInt64  // sql.NullInt64 实现了 Scanner/Valuer 接口
13}

2.3 Not 条件

作用与 Where 类似

 1db.Not("name", "jinzhu").First(&user)
 2//// SELECT * FROM users WHERE name <> "jinzhu" LIMIT 1;
 3
 4// Not In
 5db.Not("name", []string{"jinzhu", "jinzhu 2"}).Find(&users)
 6//// SELECT * FROM users WHERE name NOT IN ("jinzhu", "jinzhu 2");
 7
 8// Not In slice of primary keys
 9db.Not([]int64{1,2,3}).First(&user)
10//// SELECT * FROM users WHERE id NOT IN (1,2,3);
11
12db.Not([]int64{}).First(&user)
13//// SELECT * FROM users;
14
15// Plain SQL
16db.Not("name = ?", "jinzhu").First(&user)
17//// SELECT * FROM users WHERE NOT(name = "jinzhu");
18
19// Struct
20db.Not(User{Name: "jinzhu"}).First(&user)
21//// SELECT * FROM users WHERE name <> "jinzhu";

2.4 Or 条件

 1db.Where("role = ?", "admin").Or("role = ?", "super_admin").Find(&users)
 2//// SELECT * FROM users WHERE role = 'admin' OR role = 'super_admin';
 3
 4// Struct
 5db.Where("name = 'jinzhu'").Or(User{Name: "jinzhu 2"}).Find(&users)
 6//// SELECT * FROM users WHERE name = 'jinzhu' OR name = 'jinzhu 2';
 7
 8// Map
 9db.Where("name = 'jinzhu'").Or(map[string]interface{}{"name": "jinzhu 2"}).Find(&users)
10//// SELECT * FROM users WHERE name = 'jinzhu' OR name = 'jinzhu 2';

2.5 Inline Condition 内联条件

作用与 Where 类似

当内联条件与 多个立即执行方法 一起使用时, 内联条件不会传递给后面的立即执行方法。

 1// 根据主键获取记录 (只适用于整形主键)
 2db.First(&user, 23)
 3//// SELECT * FROM users WHERE id = 23 LIMIT 1;
 4// 根据主键获取记录, 如果它是一个非整形主键
 5db.First(&user, "id = ?", "string_primary_key")
 6//// SELECT * FROM users WHERE id = 'string_primary_key' LIMIT 1;
 7
 8// Plain SQL
 9db.Find(&user, "name = ?", "jinzhu")
10//// SELECT * FROM users WHERE name = "jinzhu";
11
12db.Find(&users, "name <> ? AND age > ?", "jinzhu", 20)
13//// SELECT * FROM users WHERE name <> "jinzhu" AND age > 20;
14
15// Struct
16db.Find(&users, User{Age: 20})
17//// SELECT * FROM users WHERE age = 20;
18
19// Map
20db.Find(&users, map[string]interface{}{"age": 20})
21//// SELECT * FROM users WHERE age = 20;

2.6 Extra Querying option 其它查询选项

1// 为查询 SQL 添加额外的 SQL 操作
2db.Set("gorm:query_option", "FOR UPDATE").First(&user, 10)
3//// SELECT * FROM users WHERE id = 10 FOR UPDATE;

2.7 FirstOrInit

获取匹配的第一条记录,否则根据给定的条件初始化一个新的对象 (仅支持 struct 和 map 条件)

1// 未找到
2db.FirstOrInit(&user, User{Name: "non_existing"})
3//// user -> User{Name: "non_existing"}
4
5// 找到
6db.Where(User{Name: "Jinzhu"}).FirstOrInit(&user)
7//// user -> User{Id: 111, Name: "Jinzhu", Age: 20}
8db.FirstOrInit(&user, map[string]interface{}{"name": "jinzhu"})
9//// user -> User{Id: 111, Name: "Jinzhu", Age: 20}

Attrs

如果记录未找到,将使用参数初始化 struct.

 1// 未找到
 2db.Where(User{Name: "non_existing"}).Attrs(User{Age: 20}).FirstOrInit(&user)
 3//// SELECT * FROM USERS WHERE name = 'non_existing';
 4//// user -> User{Name: "non_existing", Age: 20}
 5
 6db.Where(User{Name: "non_existing"}).Attrs("age", 20).FirstOrInit(&user)
 7//// SELECT * FROM USERS WHERE name = 'non_existing';
 8//// user -> User{Name: "non_existing", Age: 20}
 9
10// 找到
11db.Where(User{Name: "Jinzhu"}).Attrs(User{Age: 30}).FirstOrInit(&user)
12//// SELECT * FROM USERS WHERE name = jinzhu';
13//// user -> User{Id: 111, Name: "Jinzhu", Age: 20}

Assign

不管记录是否找到,都将参数赋值给 struct.

1// 未找到
2db.Where(User{Name: "non_existing"}).Assign(User{Age: 20}).FirstOrInit(&user)
3//// user -> User{Name: "non_existing", Age: 20}
4
5// 找到
6db.Where(User{Name: "Jinzhu"}).Assign(User{Age: 30}).FirstOrInit(&user)
7//// SELECT * FROM USERS WHERE name = jinzhu';
8//// user -> User{Id: 111, Name: "Jinzhu", Age: 30}

2.8 FirstOrCreate

获取匹配的第一条记录, 否则根据给定的条件创建一个新的记录 (仅支持 struct 和 map 条件)

1// 未找到
2db.FirstOrCreate(&user, User{Name: "non_existing"})
3//// INSERT INTO "users" (name) VALUES ("non_existing");
4//// user -> User{Id: 112, Name: "non_existing"}
5
6// 找到
7db.Where(User{Name: "Jinzhu"}).FirstOrCreate(&user)
8//// user -> User{Id: 111, Name: "Jinzhu"}

Attrs

如果记录未找到,将使用参数创建 struct 和记录.

 1// 未找到
 2db.Where(User{Name: "non_existing"}).Attrs(User{Age: 20}).FirstOrCreate(&user)
 3//// SELECT * FROM users WHERE name = 'non_existing';
 4//// INSERT INTO "users" (name, age) VALUES ("non_existing", 20);
 5//// user -> User{Id: 112, Name: "non_existing", Age: 20}
 6
 7// 找到
 8db.Where(User{Name: "jinzhu"}).Attrs(User{Age: 30}).FirstOrCreate(&user)
 9//// SELECT * FROM users WHERE name = 'jinzhu';
10//// user -> User{Id: 111, Name: "jinzhu", Age: 20}

Assign

不管记录是否找到,都将参数赋值给 struct 并保存至数据库.

 1// 未找到
 2db.Where(User{Name: "non_existing"}).Assign(User{Age: 20}).FirstOrCreate(&user)
 3//// SELECT * FROM users WHERE name = 'non_existing';
 4//// INSERT INTO "users" (name, age) VALUES ("non_existing", 20);
 5//// user -> User{Id: 112, Name: "non_existing", Age: 20}
 6
 7// 找到
 8db.Where(User{Name: "jinzhu"}).Assign(User{Age: 30}).FirstOrCreate(&user)
 9//// SELECT * FROM users WHERE name = 'jinzhu';
10//// UPDATE users SET age=30 WHERE id = 111;
11//// user -> User{Id: 111, Name: "jinzhu", Age: 30}

2.9 Advanced Query 高级查询

SubQuery 子查询

基于 *gorm.expr 的子查询

1db.Where("amount > ?", DB.Table("orders").Select("AVG(amount)").Where("state = ?", "paid").QueryExpr()).Find(&orders)
2// SELECT * FROM "orders"  WHERE "orders"."deleted_at" IS NULL AND (amount > (SELECT AVG(amount) FROM "orders"  WHERE (state = 'paid')));

Select

Select,指定你想从数据库中检索出的字段,默认会选择全部字段。

1db.Select("name, age").Find(&users)
2//// SELECT name, age FROM users;
3
4db.Select([]string{"name", "age"}).Find(&users)
5//// SELECT name, age FROM users;
6
7db.Table("users").Select("COALESCE(age,?)", 42).Rows()
8//// SELECT COALESCE(age,'42') FROM users;

Order

Order,指定从数据库中检索出记录的顺序。设置第二个参数 reorder 为 true ,可以覆盖前面定义的排序条件。

 1db.Order("age desc, name").Find(&users)
 2//// SELECT * FROM users ORDER BY age desc, name;
 3
 4// 多字段排序
 5db.Order("age desc").Order("name").Find(&users)
 6//// SELECT * FROM users ORDER BY age desc, name;
 7
 8// 覆盖排序
 9db.Order("age desc").Find(&users1).Order("age", true).Find(&users2)
10//// SELECT * FROM users ORDER BY age desc; (users1)
11//// SELECT * FROM users ORDER BY age; (users2)

Limit

Limit,指定从数据库检索出的最大记录数。

1db.Limit(3).Find(&users)
2//// SELECT * FROM users LIMIT 3;
3
4// -1 取消 Limit 条件
5db.Limit(10).Find(&users1).Limit(-1).Find(&users2)
6//// SELECT * FROM users LIMIT 10; (users1)
7//// SELECT * FROM users; (users2)

Offset

Offset,指定开始返回记录前要跳过的记录数。

1db.Offset(3).Find(&users)
2//// SELECT * FROM users OFFSET 3;
3
4// -1 取消 Offset 条件
5db.Offset(10).Find(&users1).Offset(-1).Find(&users2)
6//// SELECT * FROM users OFFSET 10; (users1)
7//// SELECT * FROM users; (users2)

Count

Count,该 model 能获取的记录总数。

 1db.Where("name = ?", "jinzhu").Or("name = ?", "jinzhu 2").Find(&users).Count(&count)
 2//// SELECT * from USERS WHERE name = 'jinzhu' OR name = 'jinzhu 2'; (users)
 3//// SELECT count(*) FROM users WHERE name = 'jinzhu' OR name = 'jinzhu 2'; (count)
 4
 5db.Model(&User{}).Where("name = ?", "jinzhu").Count(&count)
 6//// SELECT count(*) FROM users WHERE name = 'jinzhu'; (count)
 7
 8db.Table("deleted_users").Count(&count)
 9//// SELECT count(*) FROM deleted_users;
10
11db.Table("deleted_users").Select("count(distinct(name))").Count(&count)
12//// SELECT count( distinct(name) ) FROM deleted_users; (count)

注意 Count 必须是链式查询的最后一个操作 ,因为它会覆盖前面的 SELECT,但如果里面使用了 count 时不会覆盖


Group & Having

 1rows, err := db.Table("orders").Select("date(created_at) as date, sum(amount) as total").Group("date(created_at)").Rows()
 2for rows.Next() {
 3  ...
 4}
 5
 6rows, err := db.Table("orders").Select("date(created_at) as date, sum(amount) as total").Group("date(created_at)").Having("sum(amount) > ?", 100).Rows()
 7for rows.Next() {
 8  ...
 9}
10
11type Result struct {
12  Date  time.Time
13  Total int64
14}
15db.Table("orders").Select("date(created_at) as date, sum(amount) as total").Group("date(created_at)").Having("sum(amount) > ?", 100).Scan(&results)

Joins

Joins,指定连接条件

1rows, err := db.Table("users").Select("users.name, emails.email").Joins("left join emails on emails.user_id = users.id").Rows()
2for rows.Next() {
3  ...
4}
5
6db.Table("users").Select("users.name, emails.email").Joins("left join emails on emails.user_id = users.id").Scan(&results)
7
8// 多连接及参数
9db.Joins("JOIN emails ON emails.user_id = users.id AND emails.email = ?", "jinzhu@example.org").Joins("JOIN credit_cards ON credit_cards.user_id = users.id").Where("credit_cards.number = ?", "411111111111").Find(&user)

Pluck

Pluck,查询 model 中的一个列作为切片,如果您想要查询多个列,您应该使用 Scan

 1var ages []int64
 2db.Find(&users).Pluck("age", &ages)
 3
 4var names []string
 5db.Model(&User{}).Pluck("name", &names)
 6
 7db.Table("deleted_users").Pluck("name", &names)
 8
 9// 想查询多个字段? 这样做:
10db.Select("name, age").Find(&users)

Scan

Scan,扫描结果至一个 struct.

 1type Result struct {
 2  Name string
 3  Age  int
 4}
 5
 6var result Result
 7db.Table("users").Select("name, age").Where("name = ?", "Antonio").Scan(&result)
 8
 9// 原生 SQL
10db.Raw("SELECT name, age FROM users WHERE name = ?", "Antonio").Scan(&result)

三、更新

3.1 更新所有字段

Save会更新所有字段,即使你没有赋值

1db.First(&user)
2
3user.Name = "jinzhu 2"
4user.Age = 100
5db.Save(&user)
6
7//// UPDATE users SET name='jinzhu 2', age=100, birthday='2016-01-01', updated_at = '2013-11-17 21:34:10' WHERE id=111;

3.2 更新修改字段

如果你只希望更新指定字段,可以使用Update或者Updates

 1// 更新单个属性,如果它有变化
 2db.Model(&user).Update("name", "hello")
 3//// UPDATE users SET name='hello', updated_at='2013-11-17 21:34:10' WHERE id=111;
 4
 5// 根据给定的条件更新单个属性
 6db.Model(&user).Where("active = ?", true).Update("name", "hello")
 7//// UPDATE users SET name='hello', updated_at='2013-11-17 21:34:10' WHERE id=111 AND active=true;
 8
 9// 使用 map 更新多个属性,只会更新其中有变化的属性
10db.Model(&user).Updates(map[string]interface{}{"name": "hello", "age": 18, "actived": false})
11//// UPDATE users SET name='hello', age=18, actived=false, updated_at='2013-11-17 21:34:10' WHERE id=111;
12
13// 使用 struct 更新多个属性,只会更新其中有变化且为非零值的字段
14db.Model(&user).Updates(User{Name: "hello", Age: 18})
15//// UPDATE users SET name='hello', age=18, updated_at = '2013-11-17 21:34:10' WHERE id = 111;
16
17// 警告:当使用 struct 更新时,GORM只会更新那些非零值的字段
18// 对于下面的操作,不会发生任何更新,"", 0, false 都是其类型的零值
19db.Model(&user).Updates(User{Name: "", Age: 0, Actived: false})

3.3 更新选定字段

如果你想更新或忽略某些字段,你可以使用 SelectOmit

1db.Model(&user).Select("name").Updates(map[string]interface{}{"name": "hello", "age": 18, "actived": false})
2//// UPDATE users SET name='hello', updated_at='2013-11-17 21:34:10' WHERE id=111;
3
4db.Model(&user).Omit("name").Updates(map[string]interface{}{"name": "hello", "age": 18, "actived": false})
5//// UPDATE users SET age=18, actived=false, updated_at='2013-11-17 21:34:10' WHERE id=111;

3.4 无 Hooks 更新

上面的更新操作会自动运行 model 的 BeforeUpdate, AfterUpdate 方法,更新 UpdatedAt 时间戳, 在更新时保存其 Associations, 如果你不想调用这些方法,你可以使用 UpdateColumnUpdateColumns

1// 更新单个属性,类似于 `Update`
2db.Model(&user).UpdateColumn("name", "hello")
3//// UPDATE users SET name='hello' WHERE id = 111;
4
5// 更新多个属性,类似于 `Updates`
6db.Model(&user).UpdateColumns(User{Name: "hello", Age: 18})
7//// UPDATE users SET name='hello', age=18 WHERE id = 111;

3.5 批量更新

批量更新时 Hooks 不会运行

1db.Table("users").Where("id IN (?)", []int{10, 11}).Updates(map[string]interface{}{"name": "hello", "age": 18})
2//// UPDATE users SET name='hello', age=18 WHERE id IN (10, 11);
3
4// 使用 struct 更新时,只会更新非零值字段,若想更新所有字段,请使用map[string]interface{}
5db.Model(User{}).Updates(User{Name: "hello", Age: 18})
6//// UPDATE users SET name='hello', age=18;
7
8// 使用 `RowsAffected` 获取更新记录总数
9db.Model(User{}).Updates(User{Name: "hello", Age: 18}).RowsAffected

3.6 使用 SQL 表达式更新

 1DB.Model(&product).Update("price", gorm.Expr("price * ? + ?", 2, 100))
 2//// UPDATE "products" SET "price" = price * '2' + '100', "updated_at" = '2013-11-17 21:34:10' WHERE "id" = '2';
 3
 4DB.Model(&product).Updates(map[string]interface{}{"price": gorm.Expr("price * ? + ?", 2, 100)})
 5//// UPDATE "products" SET "price" = price * '2' + '100', "updated_at" = '2013-11-17 21:34:10' WHERE "id" = '2';
 6
 7DB.Model(&product).UpdateColumn("quantity", gorm.Expr("quantity - ?", 1))
 8//// UPDATE "products" SET "quantity" = quantity - 1 WHERE "id" = '2';
 9
10DB.Model(&product).Where("quantity > 1").UpdateColumn("quantity", gorm.Expr("quantity - ?", 1))
11//// UPDATE "products" SET "quantity" = quantity - 1 WHERE "id" = '2' AND quantity > 1;

3.7 修改 Hooks 中的值

如果你想修改 BeforeUpdate, BeforeSave 等 Hooks 中更新的值,你可以使用 scope.SetColumn, 例如:

1func (user *User) BeforeSave(scope *gorm.Scope) (err error) {
2  if pw, err := bcrypt.GenerateFromPassword(user.Password, 0); err == nil {
3    scope.SetColumn("EncryptedPassword", pw)
4  }
5}

3.8 其它更新选项

1// 为 update SQL 添加其它的 SQL
2db.Model(&user).Set("gorm:update_option", "OPTION (OPTIMIZE FOR UNKNOWN)").Update("name", "hello")
3//// UPDATE users SET name='hello', updated_at = '2013-11-17 21:34:10' WHERE id=111 OPTION (OPTIMIZE FOR UNKNOWN);

四、删除

4.1 删除记录


警告 删除记录时,请确保主键字段有值,GORM 会通过主键去删除记录,如果主键为空,GORM 会删除该 model 的所有记录。


1// 删除现有记录
2db.Delete(&email)
3//// DELETE from emails where id=10;
4
5// 为删除 SQL 添加额外的 SQL 操作
6db.Set("gorm:delete_option", "OPTION (OPTIMIZE FOR UNKNOWN)").Delete(&email)
7//// DELETE from emails where id=10 OPTION (OPTIMIZE FOR UNKNOWN);

4.2 批量删除

删除全部匹配的记录

1db.Where("email LIKE ?", "%jinzhu%").Delete(Email{})
2//// DELETE from emails where email LIKE "%jinzhu%";
3
4db.Delete(Email{}, "email LIKE ?", "%jinzhu%")
5//// DELETE from emails where email LIKE "%jinzhu%";

4.3 软删除

如果一个 model 有 DeletedAt 字段,他将自动获得软删除的功能! 当调用 Delete 方法时, 记录不会真正的从数据库中被删除, 只会将DeletedAt 字段的值会被设置为当前时间

 1db.Delete(&user)
 2//// UPDATE users SET deleted_at="2013-10-29 10:23" WHERE id = 111;
 3
 4// 批量删除
 5db.Where("age = ?", 20).Delete(&User{})
 6//// UPDATE users SET deleted_at="2013-10-29 10:23" WHERE age = 20;
 7
 8// 查询记录时会忽略被软删除的记录
 9db.Where("age = 20").Find(&user)
10//// SELECT * FROM users WHERE age = 20 AND deleted_at IS NULL;
11
12// Unscoped 方法可以查询被软删除的记录
13db.Unscoped().Where("age = 20").Find(&users)
14//// SELECT * FROM users WHERE age = 20;

4.4 物理删除

1// Unscoped 方法可以物理删除记录
2db.Unscoped().Delete(&order)
3//// DELETE FROM orders WHERE id=10;