i have this struct
type MerchantCsvMapping struct {
gorm.Model
MerchantID int
Name string
NameDe string
ProductNo string
PriceOld string
Ean string
Price string
Category int
DeepLink string
ShortDescription string
LongDescription string
BrandMerchant int
MerchantImageURL string
AlternateImage string
GalleryImage int
GalleryImage2 int
GalleryImage3 int
GalleryImage4 int
DeliveryTime string
DeliveryCost string
}
the table structure is
1 id bigint unsigned NULL NULL NO NULL auto_increment
2 merchant_id bigint unsigned NULL NULL NO NULL merchants(id)
3 name int unsigned NULL NULL YES NULL
4 product_no int unsigned NULL NULL YES NULL
5 price_old bigint unsigned NULL NULL YES NULL
6 ean int unsigned NULL NULL YES NULL
7 price bigint unsigned NULL NULL YES NULL
8 category int unsigned NULL NULL YES NULL
9 deep_link int unsigned NULL NULL YES NULL
10 short_description int unsigned NULL NULL YES NULL
11 long_description int unsigned NULL NULL YES NULL
12 brand_merchant int unsigned NULL NULL YES NULL
13 merchant_image_url int unsigned NULL NULL YES NULL
14 alternate_image int unsigned NULL NULL YES NULL
15 gallery_image bigint unsigned NULL NULL YES NULL
16 gallery_image2 bigint unsigned NULL NULL YES NULL
17 gallery_image3 bigint unsigned NULL NULL YES NULL
18 gallery_image4 bigint unsigned NULL NULL YES NULL
19 delivery_time int unsigned NULL NULL YES NULL
20 delivery_cost int unsigned NULL NULL YES NULL
21 created_at timestamp NULL NULL YES NULL
22 updated_at timestamp NULL NULL YES NULL
23 deleted_at timestamp NULL NULL YES NULL ```
when i run this command with Feed.MerchantID = 1
db.First(&merchant_csv_mapping, Feed.MerchantID)
&merchant_csv is always merchant 29, as it has ID 1 and merchant_id 29
but i want to pick the entry for merchant with merchant_id = 1
it seems like gorm searches primarily for ID, not for merchant_id
i am now tackling this for hours :(
Maybe anyone of you has an idea
Regards Adrian
CodePudding user response:
In your example, First
only receives an integer. GORM will compare that to the primary key, unless you further specify the condition.
Use this instead:
db.First(&merchant_csv_mapping, "merchant_id = ?", Feed.MerchantID)