Your Question
Hi Colleagues, I am trying to preload a slice of models as per documented. I have a has-many relationship in the following format:
type Trip struct { gorm.Model
json:"-"TripID stringjson:"trip_id,omitempty" gorm:"column:trip_id;not null;uniqueIndex:idx_gtfs_trips_trip_id"StopTimes []StopTimejson:"stop_times" gorm:"foreignKey:TripID"}type StopTime struct { gorm.Model
json:"-"TripID stringjson:"trip_id" gorm:"column:trip_id;not null;uniqueIndex:idx_stop_time_unq;index:idx_stop_time_trip_id"Trip *Tripgorm:"references:TripID"}
This looks to me as the case you have marked in the Override Foreign Key section of Has Many, however, when I try to retrieve a dataset with preloaded StopTimes, the query that is sent by gorm tries to match StopTimes by the Trip's ID (part of the gorm.Model) instead of the TripID I have specified as a foreign key. This does not produce the correct relations.
I am querying also as documented:
var output []entities.Trip err := r.DB.GetDB().Preload("StopTimes").Find(&output).Error
I have tried changing the TripID field's name as the documentation states that by default, that is automatically concatenated (aka Trip + ID) but that produces the same results.
The document you expected this should be explained
Expected answer
I am wondering if my model has not been annotated correctly or this is an actual bug?
Comment From: github-actions[bot]
This issue has been automatically marked as stale because it has been open 360 days with no activity. Remove stale label or comment or this will be closed in 180 days