GORM Playground Link

https://github.com/go-gorm/playground/pull/90

Description

GORM v0.2.33

I have a struct like this:

type User struct {
    ID           int32 `gorm:"primaryKey"`
    Name         string
    CreatedBy    *int32
    Creator      *User `gorm:"foreignKey:CreatedBy;references:ID"`
}

and 2 rows in my db table: | id | name | created_by | |:-:|---|---| | 1 | Ali | null | | 2 | Reza | 1 |

When i get user 1 with Preload("Creator").First(u, 1) CreatedBy is nil but Creator is user 2. And when i get user 2 with Preload("Creator").First(u, 2) CreatedBy is 1 but Creator is nil.

Comment From: jinzhu

Fixed, thank you for your report.

Comment From: chinmayb

can this fix be backported to jinzhu/gorm ?

Comment From: vabshere

@chinmayb I tested the equivalent apis for this on gorm:v1. It works rather well. What you need to do, considering the above example, is add a tag of the following form : gorm:"foreignkey:ID;association_foreignkey:CreatedBy".

Comment From: shamuhammetylyas

GORM Playground Link

go-gorm/playground#90

Description

GORM v0.2.33

I have a struct like this:

go type User struct { ID int32 `gorm:"primaryKey"` Name string CreatedBy *int32 Creator *User `gorm:"foreignKey:CreatedBy;references:ID"` }

and 2 rows in my db table:

id name created_by 1 Ali null 2 Reza 1 When i get user 1 with Preload("Creator").First(u, 1) CreatedBy is nil but Creator is user 2. And when i get user 2 with Preload("Creator").First(u, 2) CreatedBy is 1 but Creator is nil.

same problem, did you solve this problem. Please reply if you solved.