Select, Omit, Preload supports clause.Associations

This commit is contained in:
Jinzhu 2020-06-20 17:21:01 +08:00
parent a1e35bdc94
commit 5883490aa7
4 changed files with 65 additions and 11 deletions

View File

@ -19,10 +19,11 @@ func SelectAndOmitColumns(stmt *gorm.Statement, requireCreate, requireUpdate boo
for _, dbName := range stmt.Schema.DBNames {
results[dbName] = true
}
break
}
if field := stmt.Schema.LookUpField(column); field != nil && field.DBName != "" {
} else if column == clause.Associations {
for _, rel := range stmt.Schema.Relationships.Relations {
results[rel.Name] = true
}
} else if field := stmt.Schema.LookUpField(column); field != nil && field.DBName != "" {
results[field.DBName] = true
} else {
results[column] = true
@ -31,7 +32,11 @@ func SelectAndOmitColumns(stmt *gorm.Statement, requireCreate, requireUpdate boo
// omit columns
for _, omit := range stmt.Omits {
if field := stmt.Schema.LookUpField(omit); field != nil && field.DBName != "" {
if omit == clause.Associations {
for _, rel := range stmt.Schema.Relationships.Relations {
results[rel.Name] = false
}
} else if field := stmt.Schema.LookUpField(omit); field != nil && field.DBName != "" {
results[field.DBName] = false
} else {
results[omit] = false

View File

@ -140,9 +140,17 @@ func Preload(db *gorm.DB) {
if db.Error == nil && len(db.Statement.Preloads) > 0 {
preloadMap := map[string][]string{}
for name := range db.Statement.Preloads {
preloadFields := strings.Split(name, ".")
for idx := range preloadFields {
preloadMap[strings.Join(preloadFields[:idx+1], ".")] = preloadFields[:idx+1]
if name == clause.Associations {
for _, rel := range db.Statement.Schema.Relationships.Relations {
if rel.Schema == db.Statement.Schema {
preloadMap[rel.Name] = []string{rel.Name}
}
}
} else {
preloadFields := strings.Split(name, ".")
for idx := range preloadFields {
preloadMap[strings.Join(preloadFields[:idx+1], ".")] = preloadFields[:idx+1]
}
}
}

View File

@ -6,6 +6,7 @@ import (
"github.com/jinzhu/now"
"gorm.io/gorm"
"gorm.io/gorm/clause"
. "gorm.io/gorm/utils/tests"
)
@ -282,13 +283,30 @@ func TestOmitWithCreate(t *testing.T) {
user := *GetUser("omit_create", Config{Account: true, Pets: 3, Toys: 3, Company: true, Manager: true, Team: 3, Languages: 3, Friends: 4})
DB.Omit("Account", "Toys", "Manager", "Birthday").Create(&user)
var user2 User
DB.Preload("Account").Preload("Pets").Preload("Toys").Preload("Company").Preload("Manager").Preload("Team").Preload("Languages").Preload("Friends").First(&user2, user.ID)
var result User
DB.Preload("Account").Preload("Pets").Preload("Toys").Preload("Company").Preload("Manager").Preload("Team").Preload("Languages").Preload("Friends").First(&result, user.ID)
user.Birthday = nil
user.Account = Account{}
user.Toys = nil
user.Manager = nil
CheckUser(t, user2, user)
CheckUser(t, result, user)
user2 := *GetUser("omit_create", Config{Account: true, Pets: 3, Toys: 3, Company: true, Manager: true, Team: 3, Languages: 3, Friends: 4})
DB.Omit(clause.Associations).Create(&user2)
var result2 User
DB.Preload(clause.Associations).First(&result2, user2.ID)
user2.Account = Account{}
user2.Toys = nil
user2.Manager = nil
user2.Company = Company{}
user2.Pets = nil
user2.Team = nil
user2.Languages = nil
user2.Friends = nil
CheckUser(t, result2, user2)
}

View File

@ -9,6 +9,29 @@ import (
. "gorm.io/gorm/utils/tests"
)
func TestPreloadWithAssociations(t *testing.T) {
var user = *GetUser("preload_with_associations", Config{
Account: true,
Pets: 2,
Toys: 3,
Company: true,
Manager: true,
Team: 4,
Languages: 3,
Friends: 1,
})
if err := DB.Create(&user).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
}
CheckUser(t, user, user)
var user2 User
DB.Preload(clause.Associations).Find(&user2, "id = ?", user.ID)
CheckUser(t, user2, user)
}
func TestNestedPreload(t *testing.T) {
var user = *GetUser("nested_preload", Config{Pets: 2})