英文:
Gorm always searches by ID, not by merchant_id
问题
我有这个结构体
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
}
表结构如下
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
当我使用 Feed.MerchantID = 1 运行以下命令时
db.First(&merchant_csv_mapping, Feed.MerchantID)
&merchant_csv_mapping 总是商家 29,因为它的 ID 是 1,merchant_id 是 29
但我想选择 merchant_id = 1 的商家条目
看起来 gorm 主要是根据 ID 而不是 merchant_id 进行搜索
我已经为此努力了几个小时
也许你们中的任何人有什么想法
问候
Adrian
英文:
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
答案1
得分: 2
在你的示例中,First
只接收一个整数。GORM 将把它与主键进行比较,除非你进一步指定条件。
请改用以下代码:
db.First(&merchant_csv_mapping, "merchant_id = ?", Feed.MerchantID)
英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论