Debian Codegraph @

tests/multi_primary_keys_test.go

raw
1 package tests_test
2
3 import (
4 "reflect"
5 "sort"
6 "testing"
7
8 "gorm.io/gorm"
9 . "gorm.io/gorm/utils/tests"
10 )
11
12 type Blog struct {
13 ID uint `gorm:"primary_key"`
14 Locale string `gorm:"primary_key"`
15 Subject string
16 Body string
17 Tags []Tag `gorm:"many2many:blog_tags;"`
18 SharedTags []Tag `gorm:"many2many:shared_blog_tags;ForeignKey:id;References:id"`
19 LocaleTags []Tag `gorm:"many2many:locale_blog_tags;ForeignKey:id,locale;References:id"`
20 }
21
22 type Tag struct {
23 ID uint `gorm:"primary_key"`
24 Locale string `gorm:"primary_key"`
25 Value string
26 Blogs []*Blog `gorm:"many2many:blog_tags"`
27 }
28
29 func compareTags(tags []Tag, contents []string) bool {
30 var tagContents []string
31 for _, tag := range tags {
32 tagContents = append(tagContents, tag.Value)
33 }
34 sort.Strings(tagContents)
35 sort.Strings(contents)
36 return reflect.DeepEqual(tagContents, contents)
37 }
38
39 func TestManyToManyWithMultiPrimaryKeys(t *testing.T) {
40 if name := DB.Dialector.Name(); name == "sqlite" || name == "sqlserver" {
41 t.Skip("skip sqlite, sqlserver due to it doesn't support multiple primary keys with auto increment")
42 }
43
44 if name := DB.Dialector.Name(); name == "postgres" || name == "mysql" || name == "gaussdb" {
45 stmt := gorm.Statement{DB: DB}
46 stmt.Parse(&Blog{})
47 stmt.Schema.LookUpField("ID").Unique = true
48 stmt.Parse(&Tag{})
49 stmt.Schema.LookUpField("ID").Unique = true
50 // postgers only allow unique constraint matching given keys
51 }
52
53 DB.Migrator().DropTable(&Blog{}, &Tag{}, "blog_tags", "locale_blog_tags", "shared_blog_tags")
54 if err := DB.AutoMigrate(&Blog{}, &Tag{}); err != nil {
55 t.Fatalf("Failed to auto migrate, got error: %v", err)
56 }
57
58 blog := Blog{
59 Locale: "ZH",
60 Subject: "subject",
61 Body: "body",
62 Tags: []Tag{
63 {Locale: "ZH", Value: "tag1"},
64 {Locale: "ZH", Value: "tag2"},
65 },
66 }
67
68 DB.Save(&blog)
69 if !compareTags(blog.Tags, []string{"tag1", "tag2"}) {
70 t.Fatalf("Blog should has two tags")
71 }
72
73 // Append
74 tag3 := &Tag{Locale: "ZH", Value: "tag3"}
75 DB.Model(&blog).Association("Tags").Append([]*Tag{tag3})
76
77 if !compareTags(blog.Tags, []string{"tag1", "tag2", "tag3"}) {
78 t.Fatalf("Blog should has three tags after Append")
79 }
80
81 if count := DB.Model(&blog).Association("Tags").Count(); count != 3 {
82 t.Fatalf("Blog should has 3 tags after Append, got %v", count)
83 }
84
85 var tags []Tag
86 DB.Model(&blog).Association("Tags").Find(&tags)
87 if !compareTags(tags, []string{"tag1", "tag2", "tag3"}) {
88 t.Fatalf("Should find 3 tags")
89 }
90
91 var blog1 Blog
92 DB.Preload("Tags").Find(&blog1)
93 if !compareTags(blog1.Tags, []string{"tag1", "tag2", "tag3"}) {
94 t.Fatalf("Preload many2many relations")
95 }
96
97 // Replace
98 tag5 := &Tag{Locale: "ZH", Value: "tag5"}
99 tag6 := &Tag{Locale: "ZH", Value: "tag6"}
100 DB.Model(&blog).Association("Tags").Replace(tag5, tag6)
101 var tags2 []Tag
102 DB.Model(&blog).Association("Tags").Find(&tags2)
103 if !compareTags(tags2, []string{"tag5", "tag6"}) {
104 t.Fatalf("Should find 2 tags after Replace")
105 }
106
107 if DB.Model(&blog).Association("Tags").Count() != 2 {
108 t.Fatalf("Blog should has three tags after Replace")
109 }
110
111 // Delete
112 DB.Model(&blog).Association("Tags").Delete(tag5)
113 var tags3 []Tag
114 DB.Model(&blog).Association("Tags").Find(&tags3)
115 if !compareTags(tags3, []string{"tag6"}) {
116 t.Fatalf("Should find 1 tags after Delete")
117 }
118
119 if DB.Model(&blog).Association("Tags").Count() != 1 {
120 t.Fatalf("Blog should has three tags after Delete")
121 }
122
123 DB.Model(&blog).Association("Tags").Delete(tag3)
124 var tags4 []Tag
125 DB.Model(&blog).Association("Tags").Find(&tags4)
126 if !compareTags(tags4, []string{"tag6"}) {
127 t.Fatalf("Tag should not be deleted when Delete with a unrelated tag")
128 }
129
130 // Clear
131 DB.Model(&blog).Association("Tags").Clear()
132 if DB.Model(&blog).Association("Tags").Count() != 0 {
133 t.Fatalf("All tags should be cleared")
134 }
135 }
136
137 func TestManyToManyWithCustomizedForeignKeys(t *testing.T) {
138 if name := DB.Dialector.Name(); name == "sqlite" || name == "sqlserver" {
139 t.Skip("skip sqlite, sqlserver due to it doesn't support multiple primary keys with auto increment")
140 }
141
142 if name := DB.Dialector.Name(); name == "postgres" {
143 t.Skip("skip postgres due to it only allow unique constraint matching given keys")
144 }
145 if name := DB.Dialector.Name(); name == "gaussdb" {
146 t.Skip("skip gaussdb due to it only allow unique constraint matching given keys")
147 }
148
149 DB.Migrator().DropTable(&Blog{}, &Tag{}, "blog_tags", "locale_blog_tags", "shared_blog_tags")
150 if err := DB.AutoMigrate(&Blog{}, &Tag{}); err != nil {
151 t.Fatalf("Failed to auto migrate, got error: %v", err)
152 }
153
154 blog := Blog{
155 Locale: "ZH",
156 Subject: "subject",
157 Body: "body",
158 SharedTags: []Tag{
159 {Locale: "ZH", Value: "tag1"},
160 {Locale: "ZH", Value: "tag2"},
161 },
162 }
163 DB.Save(&blog)
164
165 blog2 := Blog{
166 ID: blog.ID,
167 Locale: "EN",
168 }
169 DB.Create(&blog2)
170
171 if !compareTags(blog.SharedTags, []string{"tag1", "tag2"}) {
172 t.Fatalf("Blog should has two tags")
173 }
174
175 // Append
176 tag3 := &Tag{Locale: "ZH", Value: "tag3"}
177 DB.Model(&blog).Association("SharedTags").Append([]*Tag{tag3})
178 if !compareTags(blog.SharedTags, []string{"tag1", "tag2", "tag3"}) {
179 t.Fatalf("Blog should has three tags after Append")
180 }
181
182 if DB.Model(&blog).Association("SharedTags").Count() != 3 {
183 t.Fatalf("Blog should has three tags after Append")
184 }
185
186 if DB.Model(&blog2).Association("SharedTags").Count() != 3 {
187 t.Fatalf("Blog should has three tags after Append")
188 }
189
190 var tags []Tag
191 DB.Model(&blog).Association("SharedTags").Find(&tags)
192 if !compareTags(tags, []string{"tag1", "tag2", "tag3"}) {
193 t.Fatalf("Should find 3 tags")
194 }
195
196 DB.Model(&blog2).Association("SharedTags").Find(&tags)
197 if !compareTags(tags, []string{"tag1", "tag2", "tag3"}) {
198 t.Fatalf("Should find 3 tags")
199 }
200
201 var blog1 Blog
202 DB.Preload("SharedTags").Find(&blog1)
203 if !compareTags(blog1.SharedTags, []string{"tag1", "tag2", "tag3"}) {
204 t.Fatalf("Preload many2many relations")
205 }
206
207 tag4 := &Tag{Locale: "ZH", Value: "tag4"}
208 DB.Model(&blog2).Association("SharedTags").Append(tag4)
209
210 DB.Model(&blog).Association("SharedTags").Find(&tags)
211 if !compareTags(tags, []string{"tag1", "tag2", "tag3", "tag4"}) {
212 t.Fatalf("Should find 3 tags")
213 }
214
215 DB.Model(&blog2).Association("SharedTags").Find(&tags)
216 if !compareTags(tags, []string{"tag1", "tag2", "tag3", "tag4"}) {
217 t.Fatalf("Should find 3 tags")
218 }
219
220 // Replace
221 tag5 := &Tag{Locale: "ZH", Value: "tag5"}
222 tag6 := &Tag{Locale: "ZH", Value: "tag6"}
223 DB.Model(&blog2).Association("SharedTags").Replace(tag5, tag6)
224 var tags2 []Tag
225 DB.Model(&blog).Association("SharedTags").Find(&tags2)
226 if !compareTags(tags2, []string{"tag5", "tag6"}) {
227 t.Fatalf("Should find 2 tags after Replace")
228 }
229
230 DB.Model(&blog2).Association("SharedTags").Find(&tags2)
231 if !compareTags(tags2, []string{"tag5", "tag6"}) {
232 t.Fatalf("Should find 2 tags after Replace")
233 }
234
235 if DB.Model(&blog).Association("SharedTags").Count() != 2 {
236 t.Fatalf("Blog should has three tags after Replace")
237 }
238
239 // Delete
240 DB.Model(&blog).Association("SharedTags").Delete(tag5)
241 var tags3 []Tag
242 DB.Model(&blog).Association("SharedTags").Find(&tags3)
243 if !compareTags(tags3, []string{"tag6"}) {
244 t.Fatalf("Should find 1 tags after Delete")
245 }
246
247 if DB.Model(&blog).Association("SharedTags").Count() != 1 {
248 t.Fatalf("Blog should has three tags after Delete")
249 }
250
251 DB.Model(&blog2).Association("SharedTags").Delete(tag3)
252 var tags4 []Tag
253 DB.Model(&blog).Association("SharedTags").Find(&tags4)
254 if !compareTags(tags4, []string{"tag6"}) {
255 t.Fatalf("Tag should not be deleted when Delete with a unrelated tag")
256 }
257
258 // Clear
259 DB.Model(&blog2).Association("SharedTags").Clear()
260 if DB.Model(&blog).Association("SharedTags").Count() != 0 {
261 t.Fatalf("All tags should be cleared")
262 }
263 }
264
265 func TestManyToManyWithCustomizedForeignKeys2(t *testing.T) {
266 if name := DB.Dialector.Name(); name == "sqlite" || name == "sqlserver" {
267 t.Skip("skip sqlite, sqlserver due to it doesn't support multiple primary keys with auto increment")
268 }
269
270 if name := DB.Dialector.Name(); name == "postgres" || name == "mysql" {
271 t.Skip("skip postgres due to it only allow unique constraint matching given keys")
272 }
273
274 if name := DB.Dialector.Name(); name == "gaussdb" {
275 t.Skip("skip gaussdb due to it only allow unique constraint matching given keys")
276 }
277
278 DB.Migrator().DropTable(&Blog{}, &Tag{}, "blog_tags", "locale_blog_tags", "shared_blog_tags")
279 if err := DB.AutoMigrate(&Blog{}, &Tag{}); err != nil {
280 t.Fatalf("Failed to auto migrate, got error: %v", err)
281 }
282
283 blog := Blog{
284 Locale: "ZH",
285 Subject: "subject",
286 Body: "body",
287 LocaleTags: []Tag{
288 {Locale: "ZH", Value: "tag1"},
289 {Locale: "ZH", Value: "tag2"},
290 },
291 }
292 DB.Save(&blog)
293
294 blog2 := Blog{
295 ID: blog.ID,
296 Locale: "EN",
297 }
298 DB.Create(&blog2)
299
300 // Append
301 tag3 := &Tag{Locale: "ZH", Value: "tag3"}
302 DB.Model(&blog).Association("LocaleTags").Append([]*Tag{tag3})
303 if !compareTags(blog.LocaleTags, []string{"tag1", "tag2", "tag3"}) {
304 t.Fatalf("Blog should has three tags after Append")
305 }
306
307 if DB.Model(&blog).Association("LocaleTags").Count() != 3 {
308 t.Fatalf("Blog should has three tags after Append")
309 }
310
311 if DB.Model(&blog2).Association("LocaleTags").Count() != 0 {
312 t.Fatalf("EN Blog should has 0 tags after ZH Blog Append")
313 }
314
315 var tags []Tag
316 DB.Model(&blog).Association("LocaleTags").Find(&tags)
317 if !compareTags(tags, []string{"tag1", "tag2", "tag3"}) {
318 t.Fatalf("Should find 3 tags")
319 }
320
321 DB.Model(&blog2).Association("LocaleTags").Find(&tags)
322 if len(tags) != 0 {
323 t.Fatalf("Should find 0 tags for EN Blog")
324 }
325
326 var blog1 Blog
327 DB.Preload("LocaleTags").Find(&blog1, "locale = ? AND id = ?", "ZH", blog.ID)
328 if !compareTags(blog1.LocaleTags, []string{"tag1", "tag2", "tag3"}) {
329 t.Fatalf("Preload many2many relations")
330 }
331
332 tag4 := &Tag{Locale: "ZH", Value: "tag4"}
333 DB.Model(&blog2).Association("LocaleTags").Append(tag4)
334
335 DB.Model(&blog).Association("LocaleTags").Find(&tags)
336 if !compareTags(tags, []string{"tag1", "tag2", "tag3"}) {
337 t.Fatalf("Should find 3 tags for EN Blog")
338 }
339
340 DB.Model(&blog2).Association("LocaleTags").Find(&tags)
341 if !compareTags(tags, []string{"tag4"}) {
342 t.Fatalf("Should find 1 tags for EN Blog, but got %v", tags)
343 }
344
345 // Replace
346 tag5 := &Tag{Locale: "ZH", Value: "tag5"}
347 tag6 := &Tag{Locale: "ZH", Value: "tag6"}
348 DB.Model(&blog2).Association("LocaleTags").Replace(tag5, tag6)
349
350 var tags2 []Tag
351 DB.Model(&blog).Association("LocaleTags").Find(&tags2)
352 if !compareTags(tags2, []string{"tag1", "tag2", "tag3"}) {
353 t.Fatalf("CN Blog's tags should not be changed after EN Blog Replace")
354 }
355
356 var blog11 Blog
357 DB.Preload("LocaleTags").First(&blog11, "id = ? AND locale = ?", blog.ID, blog.Locale)
358 if !compareTags(blog11.LocaleTags, []string{"tag1", "tag2", "tag3"}) {
359 t.Fatalf("CN Blog's tags should not be changed after EN Blog Replace")
360 }
361
362 DB.Model(&blog2).Association("LocaleTags").Find(&tags2)
363 if !compareTags(tags2, []string{"tag5", "tag6"}) {
364 t.Fatalf("Should find 2 tags after Replace")
365 }
366
367 var blog21 Blog
368 DB.Preload("LocaleTags").First(&blog21, "id = ? AND locale = ?", blog2.ID, blog2.Locale)
369 if !compareTags(blog21.LocaleTags, []string{"tag5", "tag6"}) {
370 t.Fatalf("EN Blog's tags should be changed after Replace")
371 }
372
373 if DB.Model(&blog).Association("LocaleTags").Count() != 3 {
374 t.Fatalf("ZH Blog should has three tags after Replace")
375 }
376
377 if DB.Model(&blog2).Association("LocaleTags").Count() != 2 {
378 t.Fatalf("EN Blog should has two tags after Replace")
379 }
380
381 // Delete
382 DB.Model(&blog).Association("LocaleTags").Delete(tag5)
383
384 if DB.Model(&blog).Association("LocaleTags").Count() != 3 {
385 t.Fatalf("ZH Blog should has three tags after Delete with EN's tag")
386 }
387
388 if DB.Model(&blog2).Association("LocaleTags").Count() != 2 {
389 t.Fatalf("EN Blog should has two tags after ZH Blog Delete with EN's tag")
390 }
391
392 DB.Model(&blog2).Association("LocaleTags").Delete(tag5)
393
394 if DB.Model(&blog).Association("LocaleTags").Count() != 3 {
395 t.Fatalf("ZH Blog should has three tags after EN Blog Delete with EN's tag")
396 }
397
398 if DB.Model(&blog2).Association("LocaleTags").Count() != 1 {
399 t.Fatalf("EN Blog should has 1 tags after EN Blog Delete with EN's tag")
400 }
401
402 // Clear
403 DB.Model(&blog2).Association("LocaleTags").Clear()
404 if DB.Model(&blog).Association("LocaleTags").Count() != 3 {
405 t.Fatalf("ZH Blog's tags should not be cleared when clear EN Blog's tags")
406 }
407
408 if DB.Model(&blog2).Association("LocaleTags").Count() != 0 {
409 t.Fatalf("EN Blog's tags should be cleared when clear EN Blog's tags")
410 }
411
412 DB.Model(&blog).Association("LocaleTags").Clear()
413 if DB.Model(&blog).Association("LocaleTags").Count() != 0 {
414 t.Fatalf("ZH Blog's tags should be cleared when clear ZH Blog's tags")
415 }
416
417 if DB.Model(&blog2).Association("LocaleTags").Count() != 0 {
418 t.Fatalf("EN Blog's tags should be cleared")
419 }
420 }
421
422 func TestCompositePrimaryKeysAssociations(t *testing.T) {
423 type Label struct {
424 BookID *uint `gorm:"primarykey"`
425 Name string `gorm:"primarykey"`
426 Value string
427 }
428
429 type Book struct {
430 ID int
431 Name string
432 Labels []Label
433 }
434
435 DB.Migrator().DropTable(&Label{}, &Book{})
436 if err := DB.AutoMigrate(&Label{}, &Book{}); err != nil {
437 t.Fatalf("failed to migrate, got %v", err)
438 }
439
440 book := Book{
441 Name: "my book",
442 Labels: []Label{
443 {Name: "region", Value: "emea"},
444 },
445 }
446
447 DB.Create(&book)
448
449 var result Book
450 if err := DB.Preload("Labels").First(&result, book.ID).Error; err != nil {
451 t.Fatalf("failed to preload, got error %v", err)
452 }
453
454 AssertEqual(t, book, result)
455 }