Self referencing many2many relationship with Gorm

637 views
Skip to first unread message

Oscar Swanros

unread,
Jan 27, 2015, 6:31:26 PM1/27/15
to golan...@googlegroups.com
package main

import (
)

type User struct {
Id int64
Contacts []User `gorm:"many2many:user_contacts;"`
}


func main() {
fmt.Println("Hello, playground")
}

// Getting "pq: column "user_id" specified more than once"
// How to define a self-referencing relationship using gorm?

How can this be achieved?

Oscar Swanros

unread,
Jan 28, 2015, 10:44:51 AM1/28/15
to golan...@googlegroups.com
Ok, found a way, thanks to this comment on Github.

type User struct {
    Id int64
    Contacts []User `gorm:"foreignkey:user_id;associationforeignkey:contact_id;many2many:user_contacts;"`
}
Reply all
Reply to author
Forward
0 new messages