Golang gorm has many self refrence not working for me


(sadegh) #1

Why gorm has many self refrence not working for me

type Comment struct {
    gorm.Model
   Text    string     `sql:"type:text;"`
   Comments    []Comment    `gorm:"foreignkey:ID;associatdion_foreignkey:ParentID"`
   ParentID    uint
   PostID    uint
   UserID    uint
}

this is the error:

invalid association []

this is my code to give result

var comments Comment
var child []Comment
db.First(&comments, 3)
db.Model(&comments).Related(&child)
fmt.Println("comments: ", child)

(Sibert) #2

There are two camps advocating ORM or not. To add another layer upon plain SQL is (IMHO) limiting, slowing down and add extra learning time.

But on the other side there are many developers that claims the opposite.

FWIW…


(Ignacio Gómez) #3

You miswrote association_foreignkey.