类似这里的问题:
https://stackoverflow.com/questions/69990097/gorm-2-golang-auto-migrate-table-with-foreign-key
使用默认的ID,把很多细节都隐藏起来,确实可以正常运行,如果不使用默认的ID,能否有缺省全显的注释解释?
不能理解这段代码,居然无法跑起来。 ❌
type Product struct {
ProductId string `gorm:"primaryKey" `
MerchantId string
Merchant *Merchant `gorm:"foreignKey:MerchantId;references:MerchantId"`
}
type Merchant struct {
MerchantId string `gorm:"primaryKey" `
MerchantName string
Products []*Product
}
上述代码,应该符合直觉,为什么就是跑不起来?
类似的逻辑,prisma 应该可以跑起来。
https://github.com/prisma/prisma
报错如下:
ERROR: there is no unique constraint matching given keys for referenced table "tb_products" (SQLSTATE 42830)
注释掉,则可以运行起来 ✅
type Product struct {
ProductId string `gorm:"primaryKey" `
MerchantId string
Merchant *Merchant // `gorm:"foreignKey:MerchantId;references:MerchantId"` // 注释这里
}
type Merchant struct {
MerchantId string `gorm:"primaryKey" `
MerchantName string
Products []*Product
}
重命名MerchantInfo 运行失败 ❌
type Product struct {
ProductId string `gorm:"primaryKey" `
MerchantId string
MerchantInfo *Merchant `gorm:"foreignKey:MerchantId;references:MerchantId"` // 重命名这里
}
type Merchant struct {
MerchantId string `gorm:"primaryKey" `
MerchantName string
Products []*Product
}
请教大佬,应该如何让其跑起来?或者错误过文档重要的命名规范?