【发布时间】:2020-11-01 00:19:11
【问题描述】:
我正在尝试从 yaml 文件创建一个自引用的 many2many。引用被保存为 uuid,所以我试过这个:
type Activity struct {
ID uuid.UUID `yaml:"id" gorm:"type:uuid;primaryKey"`
ActivityStreamID uuid.UUID `yaml:"stream" gorm:"type:uuid"`
MaturityLevelID uuid.UUID `yaml:"level" gorm:"type:uuid"`
Title string `yaml:"title"`
Benefit string `yaml:"benefit"`
ShortDescription string `yaml:"shortDescription"`
LongDescription string `yaml:"longDescription" gorm:"type:text"`
Results StringArray `yaml:"results" gorm:"type:text[]"`
Metrics StringArray `yaml:"metrics" gorm:"type:text[]"`
Costs string `yaml:"costs"`
Personnel StringArray `yaml:"personnel" gorm:"type:text[]"`
Notes string `yaml:"notes"`
RelatedActivities []*Activities `yaml:"relatedActivities" gorm:"type:uuid;many2many:related_activities"`
Type string `yaml:"type"`
Questions []Question `gorm:"type:uuid;many2many:activity_questions;"`
}
问题在于,虽然这看起来像是 go-gorm 的正确用法,但它并没有在 yaml 中正确解组:
2020/10/31 21:10:28 Problem unmarshaling example.yml: yaml: unmarshal errors:
line 47: cannot unmarshal !!str `e17d573...` into example.Activity
exit status 1
当切换RelatedActivities 以使用uuid.UUID 时,yaml 工作正常,但在 go-gorm 中无法正确识别:
...
RelatedActivities []*uuid.UUID `yaml:"relatedActivities" gorm:"type:uuid;many2many:related_activities"`
...
我在这个例子中使用了 sqlite:
2020/10/31 21:11:51 .../populateDB.go:60 row value misused
[0.063ms] [rows:0] INSERT INTO `activities` (`id`,`activity_stream_id`,`maturity_level_id`,`title`,`benefit`,`short_description`,`long_description`,`results`,`metrics`,`costs`,`personnel`,`notes`,`related_activities`,`type`,`questions`) VALUES ("a573c126-b3e3-45fb-a9d1-d94c8158cf60","1a7ad26d-9e48-411e-95a6-3be79e1b90ea","1cb6e197-ca82-45ec-a65b-0ecbdd784720","Enforce timely patch management","Clear view","Actively.....","Develop ...","{}","{}","","{}","",("93dff7be-5f95-4f8d-87d2-4f4261002508","2bf0e192-a904-444b-8a2f-38c33256e80a","0082a76b-1a37-44d9-ab04-43bd2168e13d"),"Activity",(NULL))
2020/10/31 21:11:51 row value misused
...
很明显,这是因为自引用是 ("93dff7be-5f95-4f8d-87d2-4f4261002508","2bf0e192-a904-444b-8a2f-38c33256e80a","0082a76b-1a37-44d9-ab04-43bd2168e13d") 中的一个列表,但不是在新的 many2many 表中创建的。
接下来我应该尝试什么?
【问题讨论】: