Skip to content

Commit

Permalink
Add multi primary keys test
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Jun 1, 2020
1 parent a02cb39 commit 76b8e78
Show file tree
Hide file tree
Showing 9 changed files with 467 additions and 3 deletions.
6 changes: 3 additions & 3 deletions callbacks/preload.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func preload(db *gorm.DB, rels []*schema.Relationship, conds []interface{}) {
tx.Where(clause.IN{Column: column, Values: values}).Find(joinResults.Addr().Interface())

// convert join identity map to relation identity map
fieldValues := make([]interface{}, len(foreignFields))
joinFieldValues := make([]interface{}, len(joinForeignFields))
fieldValues := make([]interface{}, len(joinForeignFields))
joinFieldValues := make([]interface{}, len(joinRelForeignFields))
for i := 0; i < joinResults.Len(); i++ {
for idx, field := range joinForeignFields {
fieldValues[idx], _ = field.ValueOf(joinResults.Index(i))
Expand Down Expand Up @@ -94,7 +94,7 @@ func preload(db *gorm.DB, rels []*schema.Relationship, conds []interface{}) {
column, values := schema.ToQueryValues(relForeignKeys, foreignValues)
tx.Where(clause.IN{Column: column, Values: values}).Find(reflectResults.Addr().Interface(), conds...)

fieldValues := make([]interface{}, len(foreignFields))
fieldValues := make([]interface{}, len(relForeignFields))
for i := 0; i < reflectResults.Len(); i++ {
for idx, field := range relForeignFields {
fieldValues[idx], _ = field.ValueOf(reflectResults.Index(i))
Expand Down
4 changes: 4 additions & 0 deletions dialects/mssql/mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ type Dialector struct {
DSN string
}

func (dialector Dialector) Name() string {
return "mssql"
}

func Open(dsn string) gorm.Dialector {
return &Dialector{DSN: dsn}
}
Expand Down
4 changes: 4 additions & 0 deletions dialects/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func Open(dsn string) gorm.Dialector {
return &Dialector{DSN: dsn}
}

func (dialector Dialector) Name() string {
return "mysql"
}

func (dialector Dialector) Initialize(db *gorm.DB) (err error) {
// register callbacks
callbacks.RegisterDefaultCallbacks(db, &callbacks.Config{})
Expand Down
4 changes: 4 additions & 0 deletions dialects/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func Open(dsn string) gorm.Dialector {
return &Dialector{DSN: dsn}
}

func (dialector Dialector) Name() string {
return "postgres"
}

func (dialector Dialector) Initialize(db *gorm.DB) (err error) {
// register callbacks
callbacks.RegisterDefaultCallbacks(db, &callbacks.Config{
Expand Down
4 changes: 4 additions & 0 deletions dialects/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func Open(dsn string) gorm.Dialector {
return &Dialector{DSN: dsn}
}

func (dialector Dialector) Name() string {
return "sqlite"
}

func (dialector Dialector) Initialize(db *gorm.DB) (err error) {
// register callbacks
callbacks.RegisterDefaultCallbacks(db, &callbacks.Config{
Expand Down
1 change: 1 addition & 0 deletions interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

// Dialector GORM database dialector
type Dialector interface {
Name() string
Initialize(*DB) error
Migrator(db *DB) Migrator
DataTypeOf(*schema.Field) string
Expand Down
48 changes: 48 additions & 0 deletions schema/relationship_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,51 @@ func TestMany2ManyOverrideJoinForeignKey(t *testing.T) {
},
})
}

func TestMany2ManyWithMultiPrimaryKeys(t *testing.T) {
type Tag struct {
ID uint `gorm:"primary_key"`
Locale string `gorm:"primary_key"`
Value string
}

type Blog struct {
ID uint `gorm:"primary_key"`
Locale string `gorm:"primary_key"`
Subject string
Body string
Tags []Tag `gorm:"many2many:blog_tags;"`
SharedTags []Tag `gorm:"many2many:shared_blog_tags;ForeignKey:id;References:id"`
LocaleTags []Tag `gorm:"many2many:locale_blog_tags;ForeignKey:id,locale;References:id"`
}

checkStructRelation(t, &Blog{},
Relation{
Name: "Tags", Type: schema.Many2Many, Schema: "Blog", FieldSchema: "Tag",
JoinTable: JoinTable{Name: "blog_tags", Table: "blog_tags"},
References: []Reference{
{"ID", "Blog", "BlogID", "blog_tags", "", true},
{"Locale", "Blog", "BlogLocale", "blog_tags", "", true},
{"ID", "Tag", "TagID", "blog_tags", "", false},
{"Locale", "Tag", "TagLocale", "blog_tags", "", false},
},
},
Relation{
Name: "SharedTags", Type: schema.Many2Many, Schema: "Blog", FieldSchema: "Tag",
JoinTable: JoinTable{Name: "shared_blog_tags", Table: "shared_blog_tags"},
References: []Reference{
{"ID", "Blog", "BlogID", "shared_blog_tags", "", true},
{"ID", "Tag", "TagID", "shared_blog_tags", "", false},
},
},
Relation{
Name: "LocaleTags", Type: schema.Many2Many, Schema: "Blog", FieldSchema: "Tag",
JoinTable: JoinTable{Name: "locale_blog_tags", Table: "locale_blog_tags"},
References: []Reference{
{"ID", "Blog", "BlogID", "locale_blog_tags", "", true},
{"Locale", "Blog", "BlogLocale", "locale_blog_tags", "", true},
{"ID", "Tag", "TagID", "locale_blog_tags", "", false},
},
},
)
}
4 changes: 4 additions & 0 deletions tests/dummy_dialecter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
type DummyDialector struct {
}

func (DummyDialector) Name() string {
return "dummy"
}

func (DummyDialector) Initialize(*gorm.DB) error {
return nil
}
Expand Down
Loading

0 comments on commit 76b8e78

Please sign in to comment.