一对一关系建表

go
  • 01
  • 02
  • 03
  • 04
  • 05
  • 06
  • 07
  • 08
  • 09
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
package main import ( "gorm.io/gorm" "log" ) import "gorm.io/driver/mysql" type Student struct { gorm.Model Name string `gorm:"size:50;unique"` // 确保 Name 是唯一的 Sex string `gorm:"type:ENUM('男', '女');default:'男'"` Password string StudentDetail *StudentDetail `gorm:"foreignKey:StudentID;references:ID"` // 显式指定外键和关联字段 } type StudentDetail struct { ID int `gorm:"primarykey"` Email string StudentID int Student *Student `gorm:"foreignKey:StudentID;references:ID"` } func main() { dsn := mysql.Open("root:123456@tcp(127.0.0.1:3306)/mybatis_learn?charset=utf8&parseTime=True&loc=Local") db, err := gorm.Open(dsn, &gorm.Config{}) if err != nil { log.Println(err) return } db.Debug().AutoMigrate(&Student{}) db.Debug().AutoMigrate(&StudentDetail{}) }

插入student信息时,顺便填充student_detail

go
  • 01
  • 02
  • 03
  • 04
  • 05
  • 06
  • 07
  • 08
  • 09
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
package main import ( "fmt" "gorm.io/gorm" "log" ) import "gorm.io/driver/mysql" type Student struct { gorm.Model Name string `gorm:"size:50;unique"` // 确保 Name 是唯一的 Sex string `gorm:"type:ENUM('男', '女');default:'男'"` Password string StudentDetail *StudentDetail `gorm:"foreignKey:StudentID;references:ID"` // 显式指定外键和关联字段 } type StudentDetail struct { ID int `gorm:"primarykey"` Email string StudentID int Student *Student `gorm:"foreignKey:StudentID;references:ID"` } func main() { dsn := mysql.Open("root:123456@tcp(127.0.0.1:3306)/mybatis_learn?charset=utf8&parseTime=True&loc=Local") db, err := gorm.Open(dsn, &gorm.Config{}) if err != nil { log.Println(err) return } db.Create(&Student{ Name: "meowrain", Sex: "男", Password: "123456", StudentDetail: &StudentDetail{ Email: "meowrain@gmail.com", StudentID: 1, }, }) students := []Student{} db.Preload("StudentDetail").Find(&students) fmt.Println(students[0].StudentDetail) }

创建用户详情,关联用户

go
  • 01
  • 02
  • 03
  • 04
  • 05
  • 06
  • 07
  • 08
  • 09
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
package main import ( "gorm.io/gorm" "log" ) import "gorm.io/driver/mysql" type Student struct { gorm.Model Name string `gorm:"size:50;unique"` // 确保 Name 是唯一的 Sex string `gorm:"type:ENUM('男', '女');default:'男'"` Password string StudentDetail *StudentDetail `gorm:"foreignKey:StudentID;references:ID"` // 显式指定外键和关联字段 } type StudentDetail struct { ID int `gorm:"primarykey"` Email string StudentID int Student *Student `gorm:"foreignKey:StudentID;references:ID"` } func main() { dsn := mysql.Open("root:123456@tcp(127.0.0.1:3306)/mybatis_learn?charset=utf8&parseTime=True&loc=Local") db, err := gorm.Open(dsn, &gorm.Config{}) if err != nil { log.Println(err) return } err = db.Create(&Student{ Name: "jafdsfasfsdfadfadfdfas", Sex: "男", Password: "123456", }).Error if err != nil { log.Println(err) } err = db.Create(&StudentDetail{ Email: "meowrain@gdsfsdfdsmail.com", StudentID: 13}).Error if err != nil { log.Println(err) } }

删除

go
  • 01
  • 02
  • 03
  • 04
  • 05
  • 06
  • 07
  • 08
  • 09
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
package main import ( "gorm.io/gorm" "log" ) import "gorm.io/driver/mysql" type Student struct { gorm.Model Name string `gorm:"size:50;unique"` // 确保 Name 是唯一的 Sex string `gorm:"type:ENUM('男', '女');default:'男'"` Password string StudentDetail *StudentDetail `gorm:"foreignKey:StudentID;references:ID;constraint:OnDelete:CASCADE"` // 显式指定外键和关联字段,级联删除 } type StudentDetail struct { ID int `gorm:"primarykey"` Email string StudentID int Student *Student `gorm:"foreignKey:StudentID;references:ID"` } func main() { dsn := mysql.Open("root:123456@tcp(127.0.0.1:3306)/mybatis_learn?charset=utf8&parseTime=True&loc=Local") db, err := gorm.Open(dsn, &gorm.Config{}) if err != nil { log.Println(err) return } // 级联删除 var student Student db.Take(&student, 13) db.Unscoped().Delete(&student) }

这种情况下,我们使用直接删除是删除不掉的

我们加上select

go
  • 01
  • 02
  • 03
  • 04
  • 05
  • 06
  • 07
  • 08
  • 09
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
package main import ( "gorm.io/gorm" "log" ) import "gorm.io/driver/mysql" type Student struct { gorm.Model Name string `gorm:"size:50;unique"` // 确保 Name 是唯一的 Sex string `gorm:"type:ENUM('男', '女');default:'男'"` Password string StudentDetail *StudentDetail `gorm:"foreignKey:StudentID;references:ID;constraint:OnDelete:CASCADE"` // 显式指定外键和关联字段,级联删除 } type StudentDetail struct { ID int `gorm:"primarykey"` Email string StudentID int Student *Student `gorm:"foreignKey:StudentID;references:ID"` } func main() { dsn := mysql.Open("root:123456@tcp(127.0.0.1:3306)/mybatis_learn?charset=utf8&parseTime=True&loc=Local") db, err := gorm.Open(dsn, &gorm.Config{}) if err != nil { log.Println(err) return } // 级联删除 var student Student db.Take(&student, 13) db.Unscoped().Select("StudentDetail").Delete(&student) }


就能看到执行了语句,再看数据库

可以看到数据已经被从数据库里面删除了