Ich hänge seit Stunden fest und kann mir den Fehler nicht erklären: Ich habe eine neue Table welche mit der category table connected werden soll. Hierzu habe ich in meiner custom table die category_id und category_version_id Spalte, welche mit der Spalte id und version_id in der category Table verknüpft wird - Bzw. sollte ![]()
Die refrenzierte category_id existiert bereits in der category table. Sieht hier jemand den Fehler, warum ich obigen Fehler bekomme? Vielleicht sehe ich auch den Wald vor Lauter Code nicht mehr ![]()
Ich verstehe es nicht, will nicht in meinen Kopf - Denn ich habe soweit ich das sehe alles richtig ![]()
SQL Query:
INSERT INTO `development`.`news` (`id`, `teaser`, `category_id`, `category_version_id`) VALUES ('1', 'dasad', '4550C07D5EBF40BEA01F7A9351D1373C', '0FA91CE3E96A4BC2BE4BD9CE752C3425');
Error:
Cannot add or update a child row: a foreign key constraint fails (`development`.`news`, CONSTRAINT `fk.news.category_id` FOREIGN KEY (`category_id`, `category_version_id`) REFERENCES `category` (`id`, `version_id`) ON DELETE CASCADE ON UPDATE CASCADE)
Custom news table:
CREATE TABLE IF NOT EXISTS `news` (
`id` BINARY(16) NOT NULL,
`teaser` VARCHAR(255) NOT NULL,
`category_id` BINARY(16) NOT NULL,
`category_version_id` BINARY(16) NOT NULL,
`created_at` DATETIME(3) NOT NULL,
`updated_at` DATETIME(3) NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
// new migration for the keys
ALTER TABLE `news`
ADD KEY `fk.news.category_id` (`category_id`,`category_version_id`),
ADD CONSTRAINT `fk.news.category_id` FOREIGN KEY (`category_id`,`category_version_id`) REFERENCES `category` (`id`,`version_id`) ON DELETE CASCADE ON UPDATE CASCADE
Hier ist die Struktur meiner news table:
Die obige referenzierte category_id existiert bereits in der category table