how to perform a join query with field references with different names using GORM

huangapple go评论75阅读模式
英文:

how to perform a join query with field references with different names using GORM

问题

如何使用GORM执行具有不同名称的字段引用的连接查询,类似于以下查询:

SELECT * FROM tickets JOIN ticket_details ON tickets.ticket_id = ticket_details.ticket_id JOIN users ON tickets.create_user = users.user_id JOIN employees ON users.employee_id = employees.employee_id

在没有外键的情况下,tickets.create_user = users.user_id。

英文:

How to perform a join query with field references with different names using GORM
like this :

SELECT * FROM tickets JOIN ticket_details ON tickets.ticket_id = ticket_details.ticket_id JOIN users ON tickets.create_user = users.user_id JOIN employees ON users.employee_id = employees.employee_id

tickets.create_user = users.user_id without foreignkey

答案1

得分: 0

你可以使用"Joins"来实现这个功能。

https://gorm.io/docs/query.html#Joins

你提供的查询应该使用这种类型的Joins:

db.Model(...).Joins("JOIN ticket_details ON tickets.ticket_id = ticket_details.ticket_id").Joins("JOIN users ON tickets.create_user = users.user_id").Joins("JOIN employees ON users.employee_id = employees.employee_id")...

如果你想在你的模型中设置Tickets和Users之间的关系

我假设关系是User "has many" Tickets

并且模型User/Ticket的ID是int64类型

那么你可以通过设置gorm的"foreignKey"和"references"来实现

https://gorm.io/docs/has_many.html#Override-Foreign-Key

https://gorm.io/docs/has_many.html#Override-References

type Ticket struct {
    TicketId   int64 `gorm:"primaryKey"`
    CreateUser int64
}

type User struct {
    UserId  int64    `gorm:"primaryKey"`
    Tickets []Ticket `gorm:"foreignKey:CreateUser;references:UserId"`
}

然后你就可以像这样查询带有Tickets的用户

var users []User
db.Joins("Tickets").Find(&users)
英文:

you could do this by using the "Joins"

https://gorm.io/docs/query.html#Joins

the query that you provided should be done with this kind of Joins:

db.Model(...).Joins("JOIN ticket_details ON tickets.ticket_id = ticket_details.ticket_id").Joins("JOIN users ON tickets.create_user = users.user_id").Joins("JOIN employees ON users.employee_id = employees.employee_id")...

if you want to set the relation in your model between the Tickets and Users

I assume the relation is User "has many" Tickets

and the ID of the model User/Ticket is int64

then you could do this by set the gorm "foreignKey" and "references"

https://gorm.io/docs/has_many.html#Override-Foreign-Key

https://gorm.io/docs/has_many.html#Override-References

type Ticket struct {
	TicketId int64 `gorm:"primaryKey"`
	CreateUser int64
}

type User struct {
	UserId  int64    `gorm:"primaryKey"`
	Tickets []Ticket `gorm:"foreignKey:CreateUser;references:UserId"`
}

then you should be able to query users with tickets like this

var users []User
db.Joins("Tickets").Find(&users)

huangapple
  • 本文由 发表于 2021年10月14日 10:01:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/69564246.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定