[Gorm] [GO]有许多实现的切片
我需要注册一组实现相同接口的实体,但是它们必须在不同的表中注册,因为它们具有不同的结构。
我正在寻找与JPA相似的行为: Hibernate继承映射
以下示例简化了。
type Person struct {
ID uint64 `gorm:"primarykey"`
Name string
Identifier []identificable
}
type identificable interface {
GetValue() string
}
type DNI struct {
ID uint64 `gorm:"primarykey"`
Value string
PersonID uint64
}
func (dni DNI) GetValue() string {
return dni.Value
}
type DrivingLicense struct {
ID uint64 `gorm:"primarykey"`
Value string
PersonID uint64
}
func (licence DrivingLicense) GetValue() string {
return licence.Value
}
func main() {
db := getDB()
dni1 := DNI{}
dni1.Value = "testdni1"
drivingLicense1 := DrivingLicense{}
dni1.Value = "License1"
person := Person{}
person.Name = "test_name"
person.Identifier = []identificable{dni1, drivingLicense1}
result := db.Create(&person)
if result.Error != nil {
fmt.Sprintf(result.Error.Error())
}
}
I need to register a set of entities which implement the same interface, but they must be registered in different tables as they have different structures.
I am looking for behavior similar to that achieved with jpa:
Hibernate Inheritance Mapping
The following example is simplified.
type Person struct {
ID uint64 `gorm:"primarykey"`
Name string
Identifier []identificable
}
type identificable interface {
GetValue() string
}
type DNI struct {
ID uint64 `gorm:"primarykey"`
Value string
PersonID uint64
}
func (dni DNI) GetValue() string {
return dni.Value
}
type DrivingLicense struct {
ID uint64 `gorm:"primarykey"`
Value string
PersonID uint64
}
func (licence DrivingLicense) GetValue() string {
return licence.Value
}
func main() {
db := getDB()
dni1 := DNI{}
dni1.Value = "testdni1"
drivingLicense1 := DrivingLicense{}
dni1.Value = "License1"
person := Person{}
person.Name = "test_name"
person.Identifier = []identificable{dni1, drivingLicense1}
result := db.Create(&person)
if result.Error != nil {
fmt.Sprintf(result.Error.Error())
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Gorm从结构名称中派生表名称,它与接口无关。
每个结构都将放在自己的桌子上。
请提供更多有关您遇到问题的代码和详细信息。
GORM derives table names from the structure name, it has nothing to with interfaces.
Each of the structures will be put in their own table.
Please provide more code and details about exactly where you are running into a problem.