pop
pop copied to clipboard
Eager() belongs_to and ID field
Description
belongs_to associations have an issue with primary key not named id
This is a valid model
type Skin struct {
ID int64 json:"skin_id" db:"skin_id"
CreatedAt time.Time json:"created_at" db:"created_at"
UpdatedAt time.Time json:"updated_at" db:"updated_at"
Name string json:"skin_name" db:"skin_name"
}
But it won´t work with belong_to associations
Steps to Reproduce the Problem
Define the previous model Define a new model like
type Content struct {
ID int64 json:"content_id" db:"content_id"
CreatedAt time.Time json:"created_at" db:"created_at"
UpdatedAt time.Time json:"updated_at" db:"updated_at"
ContentName string json:"content_name" db:"content_name"
SkinID int64 json:"skin_id" db:"skin_id"
Skin Skin json:"skin,omitempty" belongs_to:"skin" fk_id:"skin_id" db:"-"
}
Expected Behavior
Query must be selecting using WHERE skin_id = $1
skin_id must be got from the db tag at Skin model
Actual Behavior
Query uses id = $1
Info
The reason is "simple"
at associations/belongs_to_association.go line 64 we have an if block that is wrong...
Let me try to explain because I can understand the reason...
POP allows a struct field to be named ID but have a db tag so the real DB field is named different, but it does not allow the db field to be named different when we are using a belongs_to assoc
I think that if the lib allows one thing it must allow the other one so the lines if primaryIDField != "ID" { and the close block line must be removed and run the code inside the block always