将ID和另一列设为主键(PK)。

huangapple go评论79阅读模式
英文:

Make ID and another column into PK

问题

我有一个包含许多StackRequiredParameterStack

StackRequiredParameter通过其名称进行标识,但是多个堆栈可以具有相同名称的参数。因此,主键是由其名称和所属堆栈的ID组成的复合键。

type StackRequiredParameter struct {
    ID      string `gorm:"uniqueIndex:idx_required_id_unique_per_stack"`
    StackID uint   `gorm:"uniqueIndex:idx_required_id_unique_per_stack"`
}

我尝试按照下面的方式实现Stack

type Stack struct {
    gorm.Model
    Name               string                  `gorm:"unique"`
    RequiredParameters []StackRequiredParameter `gorm:"foreignKey:ID,StackID; references:ID; constraint:OnUpdate:CASCADE,OnDelete:CASCADE" json:"requiredParameters,omitempty"`
}

但是上述代码返回以下错误:

panic: runtime error: index out of range [1] with length 1

我相当确定我之所以会出现这个错误,是因为我试图将复合键(ID,StackID)与仅堆栈的ID进行匹配。但是请告诉我是否提供了足够的信息。

问题是,我不确定应该如何进行不同的处理。

英文:

I have a Stack which has many StackRequiredParameter.

The StackRequiredParameter is identified by its name but multiple stacks can have parameters of the same name. Therefore the PK is a composite of its name and the id of the stack which it belongs to.

type StackRequiredParameter struct {
	ID      string `gorm:"uniqueIndex:idx_required_id_unique_per_stack"`
	StackID uint   `gorm:"uniqueIndex:idx_required_id_unique_per_stack"`
}

I've tried to implement the Stack as shown below.

type Stack struct {
	gorm.Model
	Name string `gorm:"unique"`
	RequiredParameters []StackRequiredParameter `gorm:"foreignKey:ID,StackID; references:ID; constraint:OnUpdate:CASCADE,OnDelete:CASCADE" json:"requiredParameters,omitempty"`

}

But the above returns the following error

panic: runtime error: index out of range [1] with length 1

I'm pretty sure I get that error because I'm trying to match a composite key (ID, StackID) with just the ID of the stack. But please let me know if I'm not posting enough information here.

The problem is though, I'm not sure how to do it differently.

答案1

得分: 0

问题是外键和引用应该包含相等的列

type Stack struct {
	gorm.Model
	Name               string                   `gorm:"unique"`
	RequiredParameters []StackRequiredParameter `gorm:"many2many:required_stack_parameters_joins; foreignKey:ID; joinForeignKey:StackID; References:Name; joinReferences:ParameterID; constraint:OnUpdate:CASCADE,OnDelete:CASCADE" json:"requiredParameters,omitempty"`
}

type StackRequiredParameter struct {
	Name string `gorm:"primaryKey"`
}
英文:

The problem is both foreign key and references should contain equal columns

type Stack struct {
	gorm.Model
	Name               string                   `gorm:"unique"`
	RequiredParameters []StackRequiredParameter `gorm:"many2many:required_stack_parameters_joins; foreignKey:ID; joinForeignKey:StackID; References:Name; joinReferences:ParameterID; constraint:OnUpdate:CASCADE,OnDelete:CASCADE" json:"requiredParameters,omitempty"`
}

type StackRequiredParameter struct {
	Name string `gorm:"primaryKey"`
}

huangapple
  • 本文由 发表于 2022年5月17日 19:31:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/72273282.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定