Skip to content

Commit

Permalink
fix: skip append relation field to default db value (#5885)
Browse files Browse the repository at this point in the history
* fix: relation field returning

* chore: gofumpt style
  • Loading branch information
a631807682 committed Dec 1, 2022
1 parent f931def commit d9525d4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion schema/schema.go
Expand Up @@ -230,7 +230,7 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam
}

for _, field := range schema.Fields {
if field.HasDefaultValue && field.DefaultValueInterface == nil {
if field.DataType != "" && field.HasDefaultValue && field.DefaultValueInterface == nil {
schema.FieldsWithDefaultDBValue = append(schema.FieldsWithDefaultDBValue, field)
}
}
Expand Down
26 changes: 26 additions & 0 deletions tests/associations_belongs_to_test.go
Expand Up @@ -3,6 +3,7 @@ package tests_test
import (
"testing"

"gorm.io/gorm"
. "gorm.io/gorm/utils/tests"
)

Expand Down Expand Up @@ -224,3 +225,28 @@ func TestBelongsToAssociationForSlice(t *testing.T) {
AssertAssociationCount(t, users[0], "Company", 0, "After Delete")
AssertAssociationCount(t, users[1], "Company", 1, "After other user Delete")
}

func TestBelongsToDefaultValue(t *testing.T) {
type Org struct {
ID string
}
type BelongsToUser struct {
OrgID string
Org Org `gorm:"default:NULL"`
}

tx := DB.Session(&gorm.Session{})
tx.Config.DisableForeignKeyConstraintWhenMigrating = true
AssertEqual(t, DB.Config.DisableForeignKeyConstraintWhenMigrating, false)

tx.Migrator().DropTable(&BelongsToUser{}, &Org{})
tx.AutoMigrate(&BelongsToUser{}, &Org{})

user := &BelongsToUser{
Org: Org{
ID: "BelongsToUser_Org_1",
},
}
err := DB.Create(&user).Error
AssertEqual(t, err, nil)
}

0 comments on commit d9525d4

Please sign in to comment.