gorm多表
01 一对一
has on
1 建立关系
例子:用户有一个节点
package node
import "github.com/jinzhu/gorm"
type User struct {
gorm.Model
Node Node
}
type Node struct {
gorm.Model
Message string
UserID uint // 是外键
}
默认是把属于的表的表名加主键作为外键:UserID
2 操作
- 新增数据
当两个表的数据同时添加时,即:User
和Node
两条数据都不存在时
package main
import (
"github.com/gin-gonic/gin"
)
func main (){
node := Node{
Message: "这是第一个Node",
}
user := User{
Node: node,
}
DB.Create(&user) // 会同时创建user数据和node数据,且会把user数据的id自动填充到Node表的UserId字段
}
当先有用户再有node时
package main
import (
"github.com/gin-gonic/gin"
)
func main (){
var user User
DB.Where("id=?", 3).First(&user)
card := CreditCard{
Number: "这是第四张卡",
UserID: user.ID,
}
DB.Create(&card)
}
- 查询数据
Related()
package main
import (
"github.com/gin-gonic/gin"
)
func main (){
id := 1
var user User
DB.Where("id=?", id).Find(&user) // 先查询出user,再查询
// select * from credit_card where user_id= user.id
DB.Model(&user).Related(&user.CreditCard, "CreditCard")
}
注意:
要先查询user
再继续查询
如果查询多条
package main
import (
"github.com/gin-gonic/gin"
)
func main (){
var user []User
DB.Where("").Find(&user)
for key, value := range user {
var card CreditCard
DB.Where("user_id=?", value.ID).Find(&card)
value.CreditCard = card
user[key] = value
}
}
- 修改
package main
import (
"github.com/gin-gonic/gin"
)
func main (){
id := 1
number := "这是第二张卡"
fmt.Println(number)
var user User
DB.Where("id=?", id).First(&user)
user.CreditCard.Number = number // 链式操作
DB.Save(&user)
}
- 删除
package main
import (
"github.com/gin-gonic/gin"
)
func main (){
id := 1
var user User
var card CreditCard
DB.Where("id=?", id).First(&user)
DB.Where("user_id=?", user.ID).Delete(&card) // 删除
}
02 一对多
has many
1 创建关系
package main
import "github.com/jinzhu/gorm"
type User struct {
gorm.Model
CreditCard CreditCard
Node []Node // 一对多
Name string
}
type Node struct {
gorm.Model
UserID uint // 一对多,外键放在多的里边,其他意思和一对一一样
Name string
}
2 操作
- 新增
package main
import (
"github.com/gin-gonic/gin"
)
func main (){
id := 4
var user User
DB.Where("id=?", id).First(&user) // 先查询出user
var node Node
node.Name = "第一个一对多"
node.UserID = user.ID
DB.Create(&node) // 创建多的数据
}
- 查询
package main
import (
"github.com/gin-gonic/gin"
)
func main (){
id := 4
var user User
DB.Where("id=?", id).First(&user)
var node []Node
DB.Model(&user).Related(&node)
}
- 修改
package main
import (
"github.com/gin-gonic/gin"
)
func main (){
userId := 4
nodeId := 2
var user User
var node Node
DB.Where("id=?", userId).First(&user)
DB.Model(&user).Where("id=?", nodeId).Related(&node)
DB.Save(&node)
}
- 删除
package main
import (
"github.com/gin-gonic/gin"
)
func main (){
userId := 4
nodeId := 2
var user User
var node Node
DB.Where("id=?", userId).First(&user)
DB.Model(&user).Where("id=?", nodeId).Related(&node)
DB.Delete(&node)
}
03 多对多
many to many
1 创建关系
package main
import "github.com/jinzhu/gorm"
type User struct {
Name string
gorm.Model
Sites []Site `gorm:"many2many:user_sites;"` // 记住了里边的双引号不能省略
}
type Site struct {
Name string
gorm.Model
}
会自动创建user_sites
表
一般放在查询频率高的表中
2 操作
- 新增数据'
两个表中的数据一块新增时
package main
import (
"github.com/gin-gonic/gin"
)
func main (){
var user User
site1 := Site{
Name: "first",
}
site2 := Site{
Name: "second",
}
user = User{
Name: "lynn",
Sites: []Site{
site1,
site2,
},
}
DB.Create(&user)
}
一个表存在向另一个表新增时
package main
import (
"github.com/gin-gonic/gin"
)
func main (){
var user User
DB.Where("id=?", 1).First(&user)
site1 := Site{
Name: "first",
}
site2 := Site{
Name: "second",
}
DB.Model(&user).Association("Sites").Append(site2, site1) // 新增两条数据,并在第三张表中加上关联关系
}
- 查询
package main
import (
"github.com/gin-gonic/gin"
)
func main (){
var user User
DB.Where("id=?", 1).First(&user)
var site []Site
DB.Model(&user).Related(&site, "Sites") // 查询user的id是1的所有的site
}
- 修改
直接查询修改
- 删除
package main
import (
"github.com/gin-gonic/gin"
)
func main (){
var user User
DB.Where("id=?", 1).First(&user)
var site Site
DB.Where("id=?", 4).First(&site)
DB.Model(&user).Association("Sites").Delete(site)
}