指尖上的记忆指尖上的记忆
首页
  • 基础
  • Laravel框架
  • Symfony框架
  • 基础
  • Gin框架
  • 基础
  • Spring框架
  • 命令
  • Nginx
  • Ai
  • Deploy
  • Docker
  • K8s
  • Micro
  • RabbitMQ
  • Mysql
  • PostgreSsql
  • Redis
  • MongoDb
  • Html
  • Js
  • 前端
  • 后端
  • Git
  • 知识扫盲
  • Golang
🌟 gitHub
首页
  • 基础
  • Laravel框架
  • Symfony框架
  • 基础
  • Gin框架
  • 基础
  • Spring框架
  • 命令
  • Nginx
  • Ai
  • Deploy
  • Docker
  • K8s
  • Micro
  • RabbitMQ
  • Mysql
  • PostgreSsql
  • Redis
  • MongoDb
  • Html
  • Js
  • 前端
  • 后端
  • Git
  • 知识扫盲
  • Golang
🌟 gitHub

gorm数据库迁移下


type Room struct {
	Id             uint      `gorm:"primarykey;not null;auto_increment;column:id;"`
	Name           string    `gorm:"type:varchar(15);default:NULL;column:name;comment:名称"`
	Students       []Student `gorm:"foreignKey:room_id;references:id;constraint:OnUpdate:RESTRICT,OnDelete:CASCADE;"`// foreignKey 和 references 配合,用于主表
	CreatedAt      time.Time `gorm:"type:timestamp;default:NULL;column:created_at;comment:创建时间"`
	DeletedAt      time.Time `gorm:"type:timestamp;default:NULL;column:deleted_at;comment:创建时间"`
	UpdatedAt      time.Time `gorm:"type:timestamp;default:NULL;column:updated_at;comment:更新时间"`
	CreatedAdminId int       `gorm:"type:int;default:NULL;column:created_admin_id;comment:创建人ID"`
	UpdatedAdminId int       `gorm:"type:int;default:NULL;column:updated_admin_id;comment:更新人ID"`
}

type Student struct {
	Id     uint   `gorm:"primarykey;not null;auto_increment;column:id;"`
	Name   string `gorm:"type:varchar(15);default:NULL;column:name;comment:名称"`
	RoomId int    `gorm:"type:int;default:NULL;column:room_id;comment:房间ID"` //room_id 要在Student 表被声明为外键的话,必须要在 room 申明 `gorm:"foreignKey:room_id;references:id;constraint:OnUpdate:RESTRICT,OnDelete:CASCADE;"` 在执行了迁移文件以后才会将room_id声明为Student表的外键
	//RoomInfo       Room    `gorm:"foreignKey:room_id,references:id"` //这一行在迁移文件应该注释掉(否则会报错,就是互相引用的问题,导致room表和student表的创建失败),这个只是在业务逻辑操作的时候,才会有的,比如 我想查某个学生信息,同时又想知道这个学生的room
	RoomInfo       Room      `gorm:"foreignKey:room_id"` //试验证明 这里只能定义 foreignKey,不能 定义 references ,否则通过Preload 查询 报错,至于 Association 更是不行。目前还没调通 Association 的使用
	CreatedAt      time.Time `gorm:"type:timestamp;default:NULL;column:created_at;comment:创建时间"`
	DeletedAt      time.Time `gorm:"type:timestamp;default:NULL;column:deleted_at;comment:创建时间"`
	UpdatedAt      time.Time `gorm:"type:timestamp;default:NULL;column:updated_at;comment:更新时间"`
	CreatedAdminId int       `gorm:"type:int;default:NULL;column:created_admin_id;comment:创建人ID"`
	UpdatedAdminId int       `gorm:"type:int;default:NULL;column:updated_admin_id;comment:更新人ID"`
}

	//一对一迁移数据库,一对多迁移数据库,注意会有相互引用的问题,所以我建议:单独建一个 migration文件,包括所有的数据表迁移信息,而不是直接用 model来控制迁移文件
	err := common.Db.AutoMigrate(&model.Room{}, &model.Student{})
	if err != nil {
		fmt.Println("迁移错误:", err)
	}

	fmt.Println("迁移完成")


	//通过主表查询
	var rooms []model.Room
	common.Db.Preload("Students").Find(&rooms)
	fmt.Printf("rooms are:%+v/n", rooms)

	//通过附表查询
	var students []model.Student
	common.Db.Preload("RoomInfo").Find(&students)
	fmt.Printf("students is:%+v\n", students)


	//下面两种使用 Association 的方法都调不通,都会报错 foreign key 定义错误的问题,还不知道 Association 刀子应该怎么使用,目前还是只能通过 Preload调用成功
	//common.Db.Model(model.Student{}).Association("RoomInfo").Find(&room)
	//fmt.Printf("students are:%+v\n", students)

	//common.Db.Model(model.Room{}).Association("Students").Find(&students)
	//fmt.Printf("students are:%+v\n", students)


	//总结:foreignKey 和 references 放到主表和附表,它们的意义是不一样的,放到主表:foreignKey 其实是定义在附表的外键字段,references 为主表的id字段;放到附表:foreignKey 指的就是当前附表的外键,references 为主表的id(默认)[这是v2版本官网的定义,https://gorm.io/docs/belongs_to.html]
	//还有就是:gorm的Related和Association 区别: Relate是v1版本才有的方法,Association是v2(1.2.+就是v2版本)才有的方法,没有了,网上的答案 真的是让人伤不起。