0
Staff need to create a relationship between table attendance and table category, but every time I try it gives error 1215 cannot add Foreign key. What am I doing wrong? Both tables are empty.
CREATE TABLE `atendimentos` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`cliente_id` int(11) NOT NULL,
`categoria_id` int(11) NOT NULL DEFAULT '1',
`contato` varchar(100) DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`), ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
and this is table of categories:
CREATE TABLE `categorias` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`parent_id` int(11) unsigned DEFAULT NULL,
`slug` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`label` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`order` int(11) NOT NULL DEFAULT '0',
`icon` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `categorias_slug_unique` (`slug`),
KEY `categorias_parent_id_foreign` (`parent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Is João Voce trying to relate a FK to his own table? (KEY
fk_cliente
(cliente_id
)))– Clayton Tosatti
@Claytontosatti no. I’m trying to relate care.categoria_id to categories.id.
– Joao Nivaldo
already tried not to put DEFAULT "1" to categoria_id?
– Caio de Paula Silva
do you want to create the relationship already in create table? Or edit and add later?
– Rodrigo Rocha
The tables are already created so it has to be in the after. But the tables still have no contents.
– Joao Nivaldo