У меня есть вложенная структура Go, созданная следующим образом:
type MainSt struct {
Id string `gorm:"primary_key"`
Field1 string
Field2 uint8
Field3 string
Field4 string
Field5 string
Carrots []Carrot `gorm:"foreignkey:MainStId"`
Potatos []Potato `gorm:"foreignkey:MainStId"`
}
type Carrot struct {
Id string `gorm:"primary_key"`
MainStId string
Type uint8
Arguments []ArgumentsCarrot `gorm:"foreignkey:CarrotId"`
}
type Potato struct {
Id string `gorm:"primary_key"`
MainStId string
Type uint8
Arguments []ArgumentsPotato `gorm:"foreignkey:PotatoId"`
}
type ArgumentsPotato struct {
Id string `gorm:"primary_key"`
PotatoId string
Type uint8
Value string
}
type ArgumentsCarrot struct {
Id string `gorm:"primary_key"`
CarrotId string
Type uint8
Value string
}
Теперь я пытаюсь вставить ее в базу данных MySQL. Я делаю это:
db.AutoMigrate(&MainSt{})
db.AutoMigrate(&Carrot{}).AddForeignKey("main_st_id", "main_st(id)", "CASCADE", "CASCADE")
db.AutoMigrate(&Potato{}).AddForeignKey("main_st_id", "main_st(id)", "CASCADE", "CASCADE")
db.AutoMigrate(&ArgumentsPotato{}).AddForeignKey("potato_id", "potatos(id)", "CASCADE", "CASCADE")
db.AutoMigrate(&ArgumentsCarrot{}).AddForeignKey("carrot_id", "carrots(id)", "CASCADE", "CASCADE")
Затем я готовлю структуру, заполняя все, и начинаю сохранять, используя функцию db.Create()
. Но у меня есть некоторые ошибки на внешних ключах. Я установил db.LogMode(true)
, и вот журналы:
(/Users/disperato/go/src/github.com/disperato/awesomeProject2/main.go:58)
[2020-01-29 00:15:12] [69.50ms] CREATE TABLE `main_sts` (`id` varchar(255),`field1`
varchar(255),`field2` tinyint unsigned,`field3` varchar(255),`field4` varchar(255),`field5`
varchar(255) , PRIMARY KEY (`id`))
[0 rows affected or returned ]
(/Users/disperato/go/src/github.com/disperato/awesomeProject2/main.go:59)
[2020-01-29 00:15:12] [201.12ms] CREATE TABLE `carrots` (`id` varchar(255),`main_st_id`
varchar(255),`type` tinyint unsigned , PRIMARY KEY (`id`))
[0 rows affected or returned ]
(/Users/disperato/go/src/github.com/disperato/awesomeProject2/main.go:59)
[2020-01-29 00:15:13] [172.26ms] ALTER TABLE `carrots` ADD CONSTRAINT
carrots_main_st_id_main_sts_id_foreign FOREIGN KEY (main_st_id) REFERENCES main_sts(id) ON
DELETE CASCADE ON UPDATE CASCADE;
[0 rows affected or returned ]
(/Users/disperato/go/src/github.com/disperato/awesomeProject2/main.go:60)
[2020-01-29 00:15:13] [83.21ms] CREATE TABLE `potatos` (`id` varchar(255),`main_st_id`
varchar(255),`type` tinyint unsigned , PRIMARY KEY (`id`))
[0 rows affected or returned ]
(/Users/disperato/go/src/github.com/disperato/awesomeProject2/main.go:60)
[2020-01-29 00:15:13] [208.46ms] ALTER TABLE `potatos` ADD CONSTRAINT
potatos_main_st_id_main_sts_id_foreign FOREIGN KEY (main_st_id) REFERENCES main_sts(id) ON
DELETE CASCADE ON UPDATE CASCADE;
[0 rows affected or returned ]
(/Users/disperato/go/src/github.com/disperato/awesomeProject2/main.go:61)
[2020-01-29 00:15:13] [102.37ms] CREATE TABLE `arguments_potatos` (`id`
varchar(255),`potato_id` varchar(255),`type` tinyint unsigned,`value` varchar(255) , PRIMARY
KEY (`id`))
[0 rows affected or returned ]
(/Users/disperato/go/src/github.com/disperato/awesomeProject2/main.go:61)
[2020-01-29 00:15:13] [228.26ms] ALTER TABLE `arguments_potatos` ADD CONSTRAINT
arguments_potatos_potato_id_potatos_id_foreign FOREIGN KEY (potato_id) REFERENCES
potatos(id) ON DELETE CASCADE ON UPDATE CASCADE;
[0 rows affected or returned ]
(/Users/disperato/go/src/github.com/disperato/awesomeProject2/main.go:62)
[2020-01-29 00:15:13] [91.50ms] CREATE TABLE `arguments_carrots` (`id`
varchar(255),`carrot_id` varchar(255),`type` tinyint unsigned,`value` varchar(255) , PRIMARY
KEY (`id`))
[0 rows affected or returned ]
(/Users/disperato/go/src/github.com/disperato/awesomeProject2/main.go:62)
[2020-01-29 00:15:13] [188.91ms] ALTER TABLE `arguments_carrots` ADD CONSTRAINT
arguments_carrots_carrot_id_carrots_id_foreign FOREIGN KEY (carrot_id) REFERENCES carrots(id)
ON DELETE CASCADE ON UPDATE CASCADE;
[0 rows affected or returned ]
(/Users/disperato/go/src/github.com/disperato/awesomeProject2/main.go:105)
[2020-01-29 00:15:13] [0.92ms] UPDATE `main_sts` SET `field1` = 'F1', `field2` = 0,
`field3` = 'F3', `field4` = 'F4', `field5` = 'F5' WHERE `main_sts`.`id` = 'FirstId'
[0 rows affected or returned ]
(/Users/disperato/go/src/github.com/disperato/awesomeProject2/main.go:105)
[2020-01-29 00:15:13] [0.23ms] UPDATE `carrots` SET `main_st_id` = 'FirstId', `type` = 0
WHERE `carrots`.`id` = 'Middle'
[0 rows affected or returned ]
(/Users/disperato/go/src/github.com/disperato/awesomeProject2/main.go:105)
[2020-01-29 00:15:13] [0.26ms] UPDATE `arguments_carrots` SET `carrot_id` = 'Middle',
`type` = 0, `value` = 'VAL' WHERE `arguments_carrots`.`id` = 'Last'
[0 rows affected or returned ]
(/Users/disperato/go/src/github.com/disperato/awesomeProject2/main.go:105)
[2020-01-29 00:15:13] [0.32ms] SELECT * FROM `arguments_carrots` WHERE
`arguments_carrots`.`id` = 'Last' ORDER BY `arguments_carrots`.`id` ASC LIMIT 1
[0 rows affected or returned ]
(/Users/disperato/go/src/github.com/disperato/awesomeProject2/main.go:105)
[2020-01-29 00:15:13] Error 1452: Cannot add or update a child row: a foreign key constraint
fails (`mydatabase`.`arguments_carrots`, CONSTRAINT
`arguments_carrots_carrot_id_carrots_id_foreign` FOREIGN KEY (`carrot_id`) REFERENCES
`carrots` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
(/Users/disperato/go/src/github.com/disperato/awesomeProject2/main.go:105)
[2020-01-29 00:15:13] [0.44ms] INSERT INTO `arguments_carrots`
(`id`,`carrot_id`,`type`,`value`) VALUES ('Last','Middle',0,'VAL')
[0 rows affected or returned ]
(/Users/disperato/go/src/github.com/disperato/awesomeProject2/main.go:105)
[2020-01-29 00:15:13] Error 1452: Cannot add or update a child row: a foreign key constraint
fails (`mydatabase`.`arguments_carrots`, CONSTRAINT
`arguments_carrots_carrot_id_carrots_id_foreign` FOREIGN KEY (`carrot_id`) REFERENCES
`carrots` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
(/Users/disperato/go/src/github.com/disperato/awesomeProject2/main.go:105)
[2020-01-29 00:15:13] Error 1452: Cannot add or update a child row: a foreign key constraint
fails (`mydatabase`.`arguments_carrots`, CONSTRAINT
`arguments_carrots_carrot_id_carrots_id_foreign` FOREIGN KEY (`carrot_id`) REFERENCES
`carrots` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
(/Users/disperato/go/src/github.com/disperato/awesomeProject2/main.go:105)
[2020-01-29 00:15:13] Error 1452: Cannot add or update a child row: a foreign key constraint
fails (`mydatabase`.`arguments_carrots`, CONSTRAINT
`arguments_carrots_carrot_id_carrots_id_foreign` FOREIGN KEY (`carrot_id`) REFERENCES
`carrots` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
(/Users/disperato/go/src/github.com/disperato/awesomeProject2/main.go:105)
[2020-01-29 00:15:13] Error 1452: Cannot add or update a child row: a foreign key constraint
fails (`mydatabase`.`arguments_carrots`, CONSTRAINT
`arguments_carrots_carrot_id_carrots_id_foreign` FOREIGN KEY (`carrot_id`) REFERENCES
`carrots` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
2020/01/29 00:15:13 Error:Error 1452: Cannot add or update a child row: a foreign key
constraint fails (`mydatabase`.`arguments_carrots`, CONSTRAINT
`arguments_carrots_carrot_id_carrots_id_foreign` FOREIGN KEY (`carrot_id`) REFERENCES
`carrots` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
Process finished with exit code 1
Я думаю, что это происходит потому, что запрос INSERT
на Carrots
никогда не запускается. Чего мне не хватает?