Gorm: Handling many-to-many relations with *gorm.DB.Create

Created on 8 Dec 2015  路  2Comments  路  Source: go-gorm/gorm

I'm currently trying two insert two 'pictures' in table Picture. These pictures have tags, and so share a many-to-many relation with table Tag.

See here the structs:

//Picture is a default DB structure for GORM
type Picture struct {
    ID        int
    Title     string
    URL       string
    CreatedAt time.Time
    UpdatedAt time.Time

    Tags []Tag `gorm:"many2many:picture_tags;"`
}

//Tag is a default DB structure for GORM
type Tag struct {
    ID        int
    Name      string
    CreatedAt time.Time
    UpdatedAt time.Time

    Pictures []Picture `gorm:"many2many:picture_tags;"`
}

With the function MockData i'm putting test data in the Database:

//MockData creates two instants of pictures with 4 tags
func MockData(db *gorm.DB) {
    baby := Tag{
        Name: "Baby",
    }

    cat := Tag{
        Name: "Cat",
    }

    gif := Tag{
        Name: "Gif",
    }

    shit := Tag{
        Name: "Shit",
    }

    picture := Picture{
        Title: "Dancing Baby Gif",
        URL:   "https://45.media.tumblr.com/07106512835c07d2a237a4d12479c7f8/tumblr_mpg5arui7E1s1clzao1_250.gif",
        Tags:  []Tag{baby, gif, shit},
    }

    picture2 := Picture{
        Title: "Dancing Cat Gif",
        URL:   "http://rs143.pbsrc.com/albums/r146/sconti1369/Funny_Pictures_Animated_Dancing_Cat.gif~c200",
        Tags:  []Tag{cat, gif, shit},
    }

    db.Create(&picture)
    db.Create(&picture2)
}

The result should be two pictures in table Picture, and four tags in table Tag, as two tags are shared between the pictures and 2 are unique.

This is not the case. Table Picture is OK:

[
   {
      "ID":1,
      "Title":"Dancing Baby Gif",
      "URL":"https://45.media.tumblr.com/07106512835c07d2a237a4d12479c7f8/tumblr_mpg5arui7E1s1clzao1_250.gif",
      "CreatedAt":"2015-12-08T19:58:55.631519+01:00",
      "UpdatedAt":"2015-12-08T19:58:55.631519+01:00",
      "Tags":null
   },
   {
      "ID":2,
      "Title":"Dancing Cat Gif",
      "URL":"http://rs143.pbsrc.com/albums/r146/sconti1369/Funny_Pictures_Animated_Dancing_Cat.gif~c200",
      "CreatedAt":"2015-12-08T19:58:55.639515+01:00",
      "UpdatedAt":"2015-12-08T19:58:55.639515+01:00",
      "Tags":null
   }
]

(Don't look at the "Tags":null part. I'm not doing a query for them, but it works when requesting only a single ID)

The Tags table is problematic:

[
   {
      "ID":1,
      "Name":"Baby",
      "CreatedAt":"2015-12-08T19:58:55.633513+01:00",
      "UpdatedAt":"2015-12-08T19:58:55.633513+01:00",
      "Pictures":null
   },
   {
      "ID":2,
      "Name":"Gif",
      "CreatedAt":"2015-12-08T19:58:55.636512+01:00",
      "UpdatedAt":"2015-12-08T19:58:55.636512+01:00",
      "Pictures":null
   },
   {
      "ID":3,
      "Name":"Shit",
      "CreatedAt":"2015-12-08T19:58:55.638514+01:00",
      "UpdatedAt":"2015-12-08T19:58:55.638514+01:00",
      "Pictures":null
   },
   {
      "ID":4,
      "Name":"Cat",
      "CreatedAt":"2015-12-08T19:58:55.640522+01:00",
      "UpdatedAt":"2015-12-08T19:58:55.640522+01:00",
      "Pictures":null
   },
   {
      "ID":5,
      "Name":"Gif",
      "CreatedAt":"2015-12-08T19:58:55.642518+01:00",
      "UpdatedAt":"2015-12-08T19:58:55.642518+01:00",
      "Pictures":null
   },
   {
      "ID":6,
      "Name":"Shit",
      "CreatedAt":"2015-12-08T19:58:55.643519+01:00",
      "UpdatedAt":"2015-12-08T19:58:55.643519+01:00",
      "Pictures":null
   }
]

Tag 'Gif' and 'Shit' are being duplicated which they should not be. Searching for tag Gif should return 1 row, and when requesting for this ID should show two linked pictures instead of one.

Removing the Pictures []Picturegorm:"many2many:picture_tags;"`` line from Tag doesn't solve the problem, but makes it more of a hassle to search corresponding pictures from tags. The tags are still being duplicated.

Hope someone can help!

Rivalo

Most helpful comment

I've just run into this issue. I feel db.Create should have a way of using existing records in relationships such as this, provided there is a unique/unique_index field specified on (in this example) Tag.

All 2 comments

Fixed this by just creating the Pictures, then the Tags, and then using the .append method (as given in the Many2Many examples) to append the Tag to the Picture.

I've just run into this issue. I feel db.Create should have a way of using existing records in relationships such as this, provided there is a unique/unique_index field specified on (in this example) Tag.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

superwf picture superwf  路  3Comments

sredxny picture sredxny  路  3Comments

izouxv picture izouxv  路  3Comments

easonlin404 picture easonlin404  路  3Comments

youtwo123 picture youtwo123  路  3Comments