diff --git a/callbacks/helper.go b/callbacks/helper.go index 8da74690..818d9c2c 100644 --- a/callbacks/helper.go +++ b/callbacks/helper.go @@ -31,7 +31,7 @@ func SelectAndOmitColumns(stmt *gorm.Statement, requireCreate, requireUpdate boo // omit columns for _, omit := range stmt.Omits { - if field := stmt.Schema.LookUpField(omit); field != nil { + if field := stmt.Schema.LookUpField(omit); field != nil && field.DBName != "" { results[field.DBName] = false } else { results[omit] = false diff --git a/tests/create_test.go b/tests/create_test.go index 6421ca34..4b9694b6 100644 --- a/tests/create_test.go +++ b/tests/create_test.go @@ -1,7 +1,6 @@ package tests_test import ( - "fmt" "testing" "time" @@ -229,7 +228,6 @@ func TestCreateWithNowFuncOverride(t *testing.T) { NEW := DB.Session(&gorm.Session{ NowFunc: func() time.Time { - fmt.Println("11iiiin") return curTime }, }) @@ -263,3 +261,34 @@ func TestCreateWithNoGORMPrimayKey(t *testing.T) { t.Errorf("No error should happen when create a record without a GORM primary key. But in the database this primary key exists and is the union of 2 or more fields\n But got: %s", err) } } + +func TestSelectWithCreate(t *testing.T) { + user := *GetUser("select_create", Config{Account: true, Pets: 3, Toys: 3, Company: true, Manager: true, Team: 3, Languages: 3, Friends: 4}) + DB.Select("Account", "Toys", "Manager", "ManagerID", "Languages", "Name", "CreatedAt", "UpdatedAt", "Age", "Active").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) + + user.Birthday = nil + user.Pets = nil + user.Company = Company{} + user.Team = nil + user.Friends = nil + + CheckUser(t, user2, user) +} + +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) + + user.Birthday = nil + user.Account = Account{} + user.Toys = nil + user.Manager = nil + + CheckUser(t, user2, user) +}