Skip to content

Commit

Permalink
Build relationships if fields are not ignored, fix #3181
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Sep 9, 2020
1 parent f6117b7 commit f6ed895
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion schema/relationship.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func (schema *Schema) buildMany2ManyRelation(relation *Relationship, field *Fiel

// build references
for _, f := range relation.JoinTable.Fields {
if f.Creatable {
if f.Creatable || f.Readable || f.Updatable {
// use same data type for foreign keys
f.DataType = fieldsMap[f.Name].DataType
f.GORMDataType = fieldsMap[f.Name].GORMDataType
Expand Down
23 changes: 23 additions & 0 deletions schema/relationship_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,29 @@ func TestMany2ManyOverrideJoinForeignKey(t *testing.T) {
})
}

func TestBuildReadonlyMany2ManyRelation(t *testing.T) {
type Profile struct {
gorm.Model
Name string
UserRefer uint
}

type User struct {
gorm.Model
Profiles []Profile `gorm:"->;many2many:user_profile;JoinForeignKey:UserReferID;JoinReferences:ProfileRefer"`
Refer uint
}

checkStructRelation(t, &User{}, Relation{
Name: "Profiles", Type: schema.Many2Many, Schema: "User", FieldSchema: "Profile",
JoinTable: JoinTable{Name: "user_profile", Table: "user_profile"},
References: []Reference{
{"ID", "User", "UserReferID", "user_profile", "", true},
{"ID", "Profile", "ProfileRefer", "user_profile", "", false},
},
})
}

func TestMany2ManyWithMultiPrimaryKeys(t *testing.T) {
type Tag struct {
ID uint `gorm:"primary_key"`
Expand Down
4 changes: 2 additions & 2 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func Parse(dest interface{}, cacheStore *sync.Map, namer Namer) (*Schema, error)

if field.DBName != "" {
// nonexistence or shortest path or first appear prioritized if has permission
if v, ok := schema.FieldsByDBName[field.DBName]; !ok || (field.Creatable && len(field.BindNames) < len(v.BindNames)) {
if v, ok := schema.FieldsByDBName[field.DBName]; !ok || ((field.Creatable || field.Updatable || field.Readable) && len(field.BindNames) < len(v.BindNames)) {
if _, ok := schema.FieldsByDBName[field.DBName]; !ok {
schema.DBNames = append(schema.DBNames, field.DBName)
}
Expand Down Expand Up @@ -219,7 +219,7 @@ func Parse(dest interface{}, cacheStore *sync.Map, namer Namer) (*Schema, error)
if _, loaded := cacheStore.LoadOrStore(modelType, schema); !loaded {
if _, embedded := schema.cacheStore.Load(embeddedCacheKey); !embedded {
for _, field := range schema.Fields {
if field.DataType == "" && field.Creatable {
if field.DataType == "" && (field.Creatable || field.Updatable || field.Readable) {
if schema.parseRelation(field); schema.err != nil {
return schema, schema.err
}
Expand Down

0 comments on commit f6ed895

Please sign in to comment.