[英]Can I use preload to load three table in gorm?
我有下面三個表:
type struct Product {
Id int
Name string
}
type struct Order {
Id int
Name string
Status int //0-not use 1 - use
UserId int 'foreginKey:UserId'
ProductId int 'foreignKey:ProductId, reference:Id'
}
type struct User {
Id int
Name int
}
我可以預加載以加載 1 個用戶的所有產品 - 使用預加載的平均狀態為 1(因為從設備到用戶沒有外鍵)?
是的,您可以參考文檔https://gorm.io/docs/preload.html
db.Preload("Orders").Preload("Profile").Preload("Role").Find(&users)
// SELECT * FROM users;
// SELECT * FROM orders WHERE user_id IN (1,2,3,4); // has many
// SELECT * FROM profiles WHERE user_id IN (1,2,3,4); // has one
// SELECT * FROM roles WHERE id IN (4,5,6); // belongs to
或預加載所有
db.Preload(clause.Associations).Find(&users)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.