英文:
many to many in gorm v2 error on foreign key
问题
我发现在以下情况下使用Gorm定义多对多关系很困难:
features(feature_id, name, slug)
operations(operation_id, name, slug)
feature_operations(feature_id, operation_id)
type Feature struct {
FeatureID int64 `gorm:"primaryKey;column:feature_id" json:"feature_id"`
Name string `validate:"required" json:"name"`
Slug string `json:"slug"`
Status string `json:"status"`
Operations []Operation `gorm:"many2many:feature_operations;foreignKey:feature_id"`
appModels.BaseModel
}
当使用feature_id
时,我遇到了错误:
列feature_operations.feature_feature_id不存在
当使用id
时,我遇到了错误:
无效的外键:id
英文:
I'm finding it difficult to define many to many relationship using Gorm in following cases
features(feature_id, name, slug)
operations(operation_id, name, slug)
feature_operations(feature_id, operation_id)
type Feature struct {
FeatureID int64 `gorm:"primaryKey;column:feature_id" json:"feature_id"`
Name string `validate:"required" json:"name"`
Slug string `json:"slug"`
Status string `json:"status"`
Operations []Operation `gorm:"many2many:feature_operations;foreignKey:feature_id"`
appModels.BaseModel
}
When using feature_id
, I get error
> column feature_operations.feature_feature_id does not exist
When using id
, I get error
> invalid foreign key: id
答案1
得分: 3
看起来你没有按照gorm建议的约定命名你的主键列,gorm建议将主键列命名为id
。
所以在你的情况下,你的foreignKey
应该是字段的名称,并且你还需要使用References
来指定你想要引用的列。在这里可以看到示例:
https://gorm.io/docs/many_to_many.html#Override-Foreign-Key
你需要的是这样的代码:
type Feature struct {
FeatureID int64 `gorm:"primaryKey;column:feature_id"`
Name string
Slug string
Operations []Operation `gorm:"many2many:feature_operations;foreignKey:FeatureID;References:OperationID"`
}
type Operation struct {
OperationID int64 `gorm:"primaryKey;column:operation_id"`
Name string
Slug string
}
这样,连接表将会是FEATURE_OPERATIONS
,有两列FEATURE_FEATURE_ID
和OPERATION_OPERATION_ID
。
如果你不喜欢冗余的列名,那么你需要使用两个额外的属性joinForeignKey
和joinReferences
来选择你自己的列名,像这样:
gorm:"many2many:feature_operations;foreignKey:FeatureID;joinForeignKey:FeatureID;References:OperationID;joinReferences:OperationID"
所有这些额外的工作都是因为你的主键是FEATURE_ID
和OPERATION_ID
,而不仅仅是ID
。如果你可以按照约定重命名列名,你会发现使用gorm会更加容易。
英文:
Looks like you are not using the convention that gorm suggests where you name your primary key columns just id
so in your case your foreignKey
should be the name of the field and you also need to use References
to specify column that you want to reference. See the example here:
https://gorm.io/docs/many_to_many.html#Override-Foreign-Key
What you need is this:
type Feature struct {
FeatureID int64 `gorm:"primaryKey;column:feature_id"`
Name string
Slug string
Operations []Operation `gorm:"many2many:feature_operations;foreignKey:FeatureID;References:OperationID"`
}
type Operation struct {
OperationID int64 `gorm:"primaryKey;column:operation_id"`
Name string
Slug string
}
After this the join table will be FEATURE_OPERATIONS
with two columns FEATURE_FEATURE_ID
AND OPERATION_OPERATION_ID
If you dont like the redundant column names then you need to use the two additional attributes joinForeignKey
and joinReferences
to choose your own names for the columns like so:
gorm:"many2many:feature_operations;foreignKey:FeatureID;joinForeignKey:FeatureID;References:OperationID;joinReferences:OperationID"
All this extra work is needed because your primary keys are FEATURE_ID
and OPERATION_ID
instead of just ID
If you can rename the column to follow the convention, you will notice life is much easier with gorm
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论