Gorm: Wrong documentation for foreignkey vs. association_foreignkey?

Created on 7 Jun 2018  路  4Comments  路  Source: go-gorm/gorm

The documentation states http://doc.gorm.io/associations.html#belongs-to

Specify Foreign Key & Association Key

type Profile struct {
gorm.Model
Refer int
Name string
}

type User struct {
gorm.Model
Profile Profile gorm:"foreignkey:ProfileID;association_foreignkey:Refer"
ProfileID int
}

What works for me is:

type Model struct {
    Id        int64     `gorm:"type:bigserial;primary_key" json:"id"`
    CreatedAt time.Time `sql:"DEFAULT:NOW()" json:"createdAt"`
    UpdatedAt time.Time `json:"updatedAt"`
}

type AppInstance struct {
    Model
    AppId string `gorm:"unique_index"` // user readable id that can't change but identifies an app
}

type Datasource struct {
    Model
    App    *AppInstance `gorm:"foreignkey:id;association_foreignkey:AppID" json:"app"`
    AppID  int64        `gorm:"type:bigserial" json:"appId"`
}

Not sure if it has to do with the additional "AppId" field in my AppInstance, so I just left it for the example. But the usage of foreignkey and association_foreignkey seems to be wrong in the documentation.

Can you confirm and fix that inside the docs? Pretty confusing :)

gorm_v1

Most helpful comment

Also, under Has One:

Specify Foreign Key & Association Key

type Profile struct {
  gorm.Model
  Name   string
  UserID uint
}

type User struct {
  gorm.Model
  Refer   uint
  Profile Profile `gorm:"foreignkey:UserID;association_foreignkey:Refer"`
}

should be
Specify Foreign Key & Association Key

type Profile struct {
  gorm.Model
  Name   string
  UserID uint
}

type User struct {
  gorm.Model
  Refer   uint // unused, may be deleted
  Profile Profile `gorm:"foreignkey:UserID;association_foreignkey:ID"`
}

All 4 comments

Also, under Has One:

Specify Foreign Key & Association Key

type Profile struct {
  gorm.Model
  Name   string
  UserID uint
}

type User struct {
  gorm.Model
  Refer   uint
  Profile Profile `gorm:"foreignkey:UserID;association_foreignkey:Refer"`
}

should be
Specify Foreign Key & Association Key

type Profile struct {
  gorm.Model
  Name   string
  UserID uint
}

type User struct {
  gorm.Model
  Refer   uint // unused, may be deleted
  Profile Profile `gorm:"foreignkey:UserID;association_foreignkey:ID"`
}

Any update here? Can someone please correct the documentation. It's very confusing ;)

Still broken.

This issue will be automatically closed because it is marked as GORM V1 issue, we have released the public testing GORM V2 release and its documents https://v2.gorm.io/docs/ already, the testing release has been used in some production services for a while, and going to release the final version in following weeks, we are still actively collecting feedback before it, please open a new issue for any suggestion or problem, thank you

Also check out https://github.com/go-gorm/gorm/wiki/GORM-V2-Release-Note-Draft for how to use the public testing version and its changelog

Was this page helpful?
0 / 5 - 0 ratings

Related issues

izouxv picture izouxv  路  3Comments

kumarsiva07 picture kumarsiva07  路  3Comments

hypertornado picture hypertornado  路  3Comments

pjebs picture pjebs  路  3Comments

zeropool picture zeropool  路  3Comments