Gorm: self referencing of belongs-to

Created on 1 Jun 2017  路  4Comments  路  Source: go-gorm/gorm

What version of Go are you using (go version)?

go version go1.8.3 linux/amd64

Which database and its version are you using?

mysql Ver 15.1 Distrib 5.5.52-MariaDB, for Linux (x86_64) using readline 5.1

What did you do?

My Model:

type User struct {
    ID string `gorm:"primary_key"`
    BelongsToUser string
    BelongsTo *User `gorm:"ForeignKey:BelongsToUser"`
}

expecting for a belongs-to referencing but behaving as has-one referencing

Most helpful comment

Same issue here.

All 4 comments

Change to?

type User struct {
    ID string `gorm:"primary_key"`
    BelongsToID string
    BelongsTo *User
}

What if you have struct property names you can not change?

type User struct {
    UUID string `gorm:"primary_key"`
    BelongsToUUID string
    BelongsTo *User
}

I can not easily change the struct property name of BelongsToUUID to BelongsToID. Can I specify in some kind of tag?

Same issue here.

same here ...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

youtwo123 picture youtwo123  路  3Comments

alanyuen picture alanyuen  路  3Comments

koalacxr picture koalacxr  路  3Comments

pjebs picture pjebs  路  3Comments

rfyiamcool picture rfyiamcool  路  3Comments