forked from go-gorm/gorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_primary_keys_test.go
46 lines (40 loc) · 964 Bytes
/
multi_primary_keys_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package gorm_test
import (
"fmt"
"os"
"testing"
)
type Blog struct {
ID uint `gorm:"primary_key"`
Locale string `gorm:"primary_key"`
Subject string
Body string
Tags []Tag `gorm:"many2many:blog_tags;"`
}
type Tag struct {
ID uint `gorm:"primary_key"`
Locale string `gorm:"primary_key"`
Value string
}
func TestManyToManyWithMultiPrimaryKeys(t *testing.T) {
if dialect := os.Getenv("GORM_DIALECT"); dialect != "" && dialect != "sqlite" {
DB.Exec(fmt.Sprintf("drop table blog_tags;"))
DB.AutoMigrate(&Blog{}, &Tag{})
blog := Blog{
Locale: "ZH",
Subject: "subject",
Body: "body",
Tags: []Tag{
{Locale: "ZH", Value: "tag1"},
{Locale: "ZH", Value: "tag2"},
},
}
DB.Save(&blog)
DB.Model(&blog).Association("Tags").Append([]Tag{{Locale: "ZH", Value: "tag3"}})
var tags []Tag
DB.Model(&blog).Related(&tags, "Tags")
if len(tags) != 3 {
t.Errorf("should found 3 tags with blog")
}
}
}