go db.Create (& u) Records Create Default value field, or implemented using pointers Scanner / Valuer, new (string), sql.NullString {String: "", Valid: true}

1. The case where setting defaults

type User struct {
	ID uint
	Name string `gorm:"default:'夜愿'"`    //数据库 name字段设置默认值
	Age int64
}
func main() {
	//1. 连接MySql数据库
	db, err  := gorm.Open("mysql", "root:root@(xxxxxx:3306)/database_name?charset=utf8&parseTime=True&loc=Local")
	if err!= nil{
		panic(err)
	}
	defer db.Close()

	//2. 自动迁移  把结构体和数据表进行对应
	db.AutoMigrate(&User{})

	//3.创建
	//u := User{Name:"tony", Age:23}  //在代码层面创建一个User对象
	u := User{Age:38}      //在代码层面创建一个User对象

	fmt.Println(db.NewRecord(&u)) //判断主键是否为空   为true
	db.Debug().Create(&u)     // 如果结构体比较大的话  还是传指针比较合适  避免传值拷贝 消耗内存   //db.Debug().Create(&u) 打印sql语句
	fmt.Println(db.NewRecord(&u))   // 再次判断主键是否为空  为false

}

The above code a new record in the database of the default values, as shown below:
Here Insert Picture Description

2. Do not set the default value when new

1. String Pointer Type:

type User struct {
	ID uint
	Name *string `gorm:"default:'nightwish'"`    //  //使用字符串指针
	Age int64
}
func main() {
	//1. 连接MySql数据库
	db, err  := gorm.Open("mysql", "root:root@(xxxxxx:3306)/xxx?charset=utf8&parseTime=True&loc=Local")
	if err!= nil{
		panic(err)
	}
	defer db.Close()

	//2. 自动迁移  把结构体和数据表进行对应
	db.AutoMigrate(&User{})

	//3.创建
	//u := User{Age:23}  // 此时 name还会有默认值 
	u := User{Name: new(string), Age:48}      //这里Name类型必须是 new(string) 这样才能与结构体中定义的字符串指针相匹配 虽然这里还是空字符串,但它属于空字符串的指针

	fmt.Println(db.NewRecord(&u)) //判断主键是否为空   为true
	db.Debug().Create(&u)     // 如果结构体比较大的话  还是传指针比较合适  避免传值拷贝 消耗内存   //db.Debug().Create(&u) 打印sql语句
	fmt.Println(db.NewRecord(&u))   // 再次判断主键是否为空  为false

}

The above code is not recorded in the database to add a default value, as shown below:
Here Insert Picture Description
2.sql.NullString
Click NullString View source found that it implements two interfaces:
Here Insert Picture Description
Here Insert Picture Description

Here Insert Picture Description
The above code a new record in the database with a default value, as shown below:
Here Insert Picture Description
The following non-default values:
Here Insert Picture Description
The above code a non-default values in the database record added, as shown below:
Here Insert Picture Description

3. query record containing the value 0:
The fields are defined as sql.NullInt64 query Age:

type User struct {
	gorm.Model
	Name string
	Age sql.NullInt64  `gorm:"default:0"`
}
u := User{Age: sql.NullInt64{Int64:0,Valid:true}}
var users []User
db.Where(&u).Find(&users)
for index, value := range users {
	fmt.Println(index, value)
}

Here Insert Picture Description
Use pointer query:

type User struct {
	gorm.Model
	Name string
	Age *int64 `gorm:"default:0"`
}
var users []User
db.Where(&User{ Age: new(int64)}).Find(&users)
for index, value := range users {
	fmt.Println(index, value)
}
Published 100 original articles · won praise 15 · views 40000 +

Guess you like

Origin blog.csdn.net/qq_37767455/article/details/104730077