Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using a pointer for a foreign-key related field yields unexpected behavior when selecting multiple values #5414

Closed
Kamran151199 opened this issue Jun 11, 2022 · 1 comment · Fixed by #5417
Assignees
Labels
type:question general questions

Comments

@Kamran151199
Copy link

Your Question

Suppose you have database models as follows:

package storage

type Country struct {
	ID   string `json:"id" gorm:"type:uuid"`
	Name string `json:"name"`
	Code string `json:"code"`
}

type City struct {
	ID           string   `json:"id" gorm:"type:uuid"`
	Name         string   `json:"name"`
	Code         *string  `json:"code"`
	CountryId    string   `json:"country_id"`
	Country      *Country `json:"country" gorm:"references:ID"`
	IATA         *string  `json:"iata"`
	Latitude     *string  `json:"latitude"`
	Longitude    *string  `json:"longitude"`
}

The city should have a pointer to a Country model to make it easier to understand whether Country has been joined (in sql) or not (e.g. if city.Country == nil {panic("for whatever reason")} )

The problem appears when I try to get the list of all cities:

package example

var cities []storage.City 
tx.Joins("Country").Find(&cities)

Here, all the cities have been fetched from DB nicely, but the countries became the same in all the cities.
If I print out cities I get the following:

OUTPUT:

[
{
 ID:51e415ab-4301-4268-9345-deed6b1d72f6 
 Name:Bergen 
 Code:0xc0004d6ec0 
 CountryId:0bd3890c-b6b7-4b27-8071-55c8f64562bb 
 Country:{
    ID:f4e819b2-5c1a-43f9-bfa1-fe56b6ee173e 
    Name:Tschechien 
    Code:CZ
  }
 SkyScannerId:0xc0004d6ee0 
 IATA:0xc0004d6ef0 
 Latitude:0xc0004d6f00 
 Longitude:0xc0004d6f10
},
 
{
 ID:2468c7f0-0275-4bff-8b7e-4e87bfa63604 
 Name:Banská Bystrica 
 Code:0xc0004d6bc0 
 CountryId:00ba76d3-9591-4d45-a39d-f554375d790f 
 Country:{
    ID:f4e819b2-5c1a-43f9-bfa1-fe56b6ee173e 
    Name:Tschechien 
    Code:CZ
  }
 SkyScannerId:<nil> 
 IATA:<nil> 
 Latitude:0xc0004d6c00 
 Longitude:0xc0004d6c10
}, 

{
 ID:75501988-3c80-4ef9-8081-73d20cbcc29b 
 Name:Prag 
 Code:0xc0004d6a60 
 CountryId:f4e819b2-5c1a-43f9-bfa1-fe56b6ee173e 
 Country:{
    ID:f4e819b2-5c1a-43f9-bfa1-fe56b6ee173e 
    Name:Tschechien 
    Code:CZ
  } 
 SkyScannerId:0xc0004d6a90 
 IATA:0xc0004d6aa0 
 Latitude:0xc0004d6ac0 
 Longitude:0xc0004d6ad0
}
] 

Please pay attention to Country field of the outputs. In the ACTUAL OUTPUT all the cities have the same country. I think this has something to do with the pointer.
I got EXPECTED OUTPUT when I removed the pointer from Country (so *Country became Country without *). But I would like to get the same output with the pointer (*Country).
Also, please do not pay attention to the values printed out in other fields. My main focus is the Country field.

The document you expected this should be explained

Expected answer

What I expected to get is:

EXPECTED OUTPUT
[
{
 ID:51e415ab-4301-4268-9345-deed6b1d72f6 
 Name:Bergen 
 Code:0xc0004d6ec0 
 CountryId:0bd3890c-b6b7-4b27-8071-55c8f64562bb 
 Country:{
    ID:0bd3890c-b6b7-4b27-8071-55c8f64562bb 
    Name:Norwegen 
    Code:NO
   } 
 SkyScannerId:0xc0004d6ee0 
 IATA:0xc0004d6ef0 
 Latitude:0xc0004d6f00 
 Longitude:0xc0004d6f10
},
 
{
 ID:2468c7f0-0275-4bff-8b7e-4e87bfa63604 
 Name:Banská Bystrica 
 Code:0xc0004d6bc0 
 CountryId:00ba76d3-9591-4d45-a39d-f554375d790f 
 Country: {
    ID:00ba76d3-9591-4d45-a39d-f554375d790f 
    Name:Slovakei 
    Code:SK
   } 
 SkyScannerId:<nil> 
 IATA:<nil> 
 Latitude:0xc0004d6c00 
 Longitude:0xc0004d6c10
}, 

{
 ID:75501988-3c80-4ef9-8081-73d20cbcc29b 
 Name:Prag 
 Code:0xc0004d6a60 
 CountryId:f4e819b2-5c1a-43f9-bfa1-fe56b6ee173e 
 Country:{
    ID:f4e819b2-5c1a-43f9-bfa1-fe56b6ee173e 
    Name:Tschechien 
    Code:CZ
  } 
 SkyScannerId:0xc0004d6a90 
 IATA:0xc0004d6aa0 
 Latitude:0xc0004d6ac0 
 Longitude:0xc0004d6ad0
}
] 
@Kamran151199 Kamran151199 added the type:question general questions label Jun 11, 2022
@Kamran151199
Copy link
Author

I just noticed that this behavior is only related to v1.23.6 (in v1.23.5 all works fine).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question general questions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants