MySQL Error Number: 1452 (unusual case) - mysql

I tried adding a Foreign Key using this query:
ALTER TABLE `StripeBilling`
ADD CONSTRAINT `StripeBillingPatientRef`
FOREIGN KEY (PatientRef)
REFERENCES `Patient`(`PatientId`)
ON DELETE SET NULL;
My tables mysql_dump is:
CREATE TABLE IF NOT EXISTS `StripeBilling` (
...
`PatientRef` int(10) unsigned DEFAULT NULL,
...
) ENGINE=InnoDB AUTO_INCREMENT=10000000 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
ALTER TABLE `StripeBilling`
ADD PRIMARY KEY (`StripeBillingId`), ADD KEY `StripeBillingAgentRef` (`AgentRef`), ADD KEY `PatientRef` (`PatientRef`);
ALTER TABLE `StripeBilling`
MODIFY `StripeBillingId` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=10000000;
ALTER TABLE `StripeBilling`
ADD CONSTRAINT `StripeBillingAgentRef` FOREIGN KEY (`AgentRef`) REFERENCES `Agent` (`AgentId`) ON DELETE SET NULL;
And patient's table is:
CREATE TABLE IF NOT EXISTS `Patient` (
`PatientId` int(10) unsigned NOT NULL,
...
) ENGINE=InnoDB AUTO_INCREMENT=10000000 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
ALTER TABLE `Patient`
ADD PRIMARY KEY (`PatientId`), ADD UNIQUE KEY `PatientSerial` (`PatientSerial`);
ALTER TABLE `Patient`
MODIFY `PatientId` int(10) unsigned NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=10000000;
But MySQL returns an error:
Cannot add or update a child row: a foreign key constraint fails
(newportal_enc.#sql-3863_9f, CONSTRAINT StripeBillingPatientRef
FOREIGN KEY (PatientRef) REFERENCES Patient (PatientId) ON
DELETE SET NULL)
I'm confused because table newportal_enc.#sql-3863_9f does not exist in my database.
This is not due to the presence of the PatientRef index, becouse after removing of this index my issue still exists.
Why am I getting this error and how can I solve it?

Related

MySQL Cluster - Composite foreign key error: Missing index for constraint

I want to add composite foreign key to my MySQL Cluster table. When I try locally the statement is executed, but on cluster I get the following error:
Failed to add the foreign key constraint. Missing index for constraint... in the referenced table 'y'
This is the statement:
ALTER TABLE x
ADD CONSTRAINT fk_x_y
FOREIGN KEY (y_id, tenant_id)
REFERENCES y(id, tenant_id);
I have executed SHOW FULL COLUMNS FROM for both tables and both columns in each table are the same. Also I have index in y table on id, tenant_id.
MySQL Cluster version: 8.0.25-cluster
Edit 1:
SHOW CREATE TABLE results:
Table x:
CREATE TABLE `x` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`tenant_id` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_y_id_tenant_id` (`tenant_id`),
CONSTRAINT `fk_x_tenant_id` FOREIGN KEY (`tenant_id`) REFERENCES `tenant` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=ndbcluster AUTO_INCREMENT=446 DEFAULT CHARSET=utf8;
Table y:
CREATE TABLE `reference_list` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`tenant_id` bigint(20) NOT NULL,
PRIMARY KEY (`id`),
KEY `y_id_tenant_id` (`id`,`tenant_id`),
KEY `fk_y_id_tenant_id` (`tenant_id`),
CONSTRAINT `fk_y_id_tenant_id` FOREIGN KEY (`tenant_id`) REFERENCES `tenant` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=ndbcluster AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
I am aware that tenant_id is DEFAULT NULL in one table and NOT_NULL in other, but changing both to DEFAULT NULL didn't solve the problem.
After running: SHOW WARNINGS; I found out I had to add UNIQUE index on id, tenant_id columns.

How to restrict inserting duplicate pairs of values in a table?

I have the following table in MySQL DB (MariaDB):
CREATE TABLE `restrictions_projects` (
`ID` int(11) NOT NULL,
`project_id` int(11) NOT NULL,
`restriction_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `restrictions_projects`
ADD PRIMARY KEY (`ID`),
ADD KEY `project_id` (`project_id`),
ADD KEY `restriction_id` (`restriction_id`);
ALTER TABLE `restrictions_projects`
MODIFY `ID` int(11) NOT NULL AUTO_INCREMENT;
ALTER TABLE `restrictions_projects`
ADD CONSTRAINT `restrictions_prfk_1` FOREIGN KEY (`project_id`) REFERENCES `projects` (`ID`),
ADD CONSTRAINT `restrictions_prfk_2` FOREIGN KEY (`restriction_id`) REFERENCES `restrictions` (`ID`);
COMMIT;
How can I restrict inserting duplicate pairs of project_id and restriction_id?
INSERT INTO restrictions_projects (project_id,restriction_id)
VALUES (1,2) // ??? ON DUPLICATE KEY UPDATE project_id=1 AND
restriction_id=2
Add a unique constraint:
ALTER TABLE restrictions_projects
ADD CONSTRAINT unq_restrictions_2 UNIQUE(project_id, restriction_id);
You can also do this using a unique index, which is effectively the same thing.
You need composite unique key on both column
UNIQUE (project_id,restriction_id)
You should alter your table to add unique key
ALTER TABLE restrictions_projects ADD CONSTRAINT restrictions_projects_uniq UNIQUE (project_id,restriction_id)

mysql - not able to add foreign key constraint after changing column name

I was changing column name of a column having foreign key constraint, i have done this by
ALTER TABLE `city_details` DROP FOREIGN KEY `tb_city_details_ibfk_1`;
ALTER TABLE `city_details` CHANGE `city_state_id` `city_district_id` int(8) NOT NULL AFTER `city_name`;
now while adding constraint back i'm getting error
1452 - Cannot add or update a child row: a foreign key constraint fails (testdb.#sql-39a8_23, CONSTRAINT city_details_ibfk_1 FOREIGN KEY (city_district_id) REFERENCES district_details (district_id) ON DELETE CASCADE ON UPDATE CASCADE)
i have used this query to add constraint
ALTER TABLE `city_details` ADD CONSTRAINT `city_details_ibfk_1` FOREIGN KEY (`city_district_id`) REFERENCES `district_details`(`district_id`) ON UPDATE CASCADE ON DELETE CASCADE;
SHOW CREATE TABLE city_details
CREATE TABLE `city_details` (
`city_id` int(8) NOT NULL AUTO_INCREMENT,
`city_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`city_district_id` int(8) NOT NULL,
PRIMARY KEY (`city_id`),
KEY `city_state_id` (`city_district_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
Got the issue district_details table was empty but city_details table had few values, after adding values to district_details it is working fine

unable to create constraint in mysql table

I've got following tables:
node_sharing | CREATE TABLE `node_sharing` (
`user_id` int(11) NOT NULL,
`node_id` int(11) NOT NULL,
PRIMARY KEY (`user_id`,`node_id`),
KEY `fk_user_id_idx` (`user_id`),
KEY `fk_node_id_idx` (`node_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
and
users | CREATE TABLE `users` (
`id` int(11) NOT NULL,
`has_ard_access` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
I would like to create following foreign key with a constraint:
ALTER TABLE `node_sharing`
ADD CONSTRAINT `fk_user_id`
FOREIGN KEY (`user_id` )
REFERENCES `users` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION;
and MySQL returns the following error:
ERROR 1005 (HY000): Can't create table 'MY_TABLE_NAME.#sql-4d0_218' (errno: 121)
What is wrong here?
PS node_sharing has been truncated, so there are no existing records that could disable putting the constraint on.
It is a duplicate key error. Did you have a table with same name before? If so, check InnoDB internal data dictionary. If not, check if you have another constraint with same name. Constraint names should be unique.

MySQL Error : #1005 - Can't create table (errno: 150) When I try create more than 1 FK

I have this table:
CREATE TABLE IF NOT EXISTS `produtos` (
`id` int(11) NOT NULL auto_increment,
`idcatprodutos` int(11) NOT NULL,
`idcategoria` int(11) NOT NULL,
`idmarca` int(11) NOT NULL,
`nome` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
KEY `FK_produtos_2` (`idcatprodutos`),
KEY `FK_produtos_3` (`idmarca`),
KEY `FK_produtos_4` (`idcategoria`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC AUTO_INCREMENT=39 ;
and this table:
CREATE TABLE IF NOT EXISTS `sugestoes` (
`id` int(11) NOT NULL auto_increment,
`idproduto` int(11) NOT NULL,
`idsugestao1` int(11) NOT NULL,
`idsugestao2` int(11) NOT NULL,
`idsugestao3` int(11) NOT NULL,
`idsugestao4` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `FK_sugestoes_prod` (`idproduto`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=FIXED AUTO_INCREMENT=9 ;
I already have created a fk sugestoes.idproduto -> produtos.id working, but I want each of the other fields also refer to the produtos.id through new FK.
Run this command below that return MySQL Error : #1005 - Can't create table (errno: 150):
ALTER TABLE `infantile`.`sugestoes` ADD CONSTRAINT `FK_sugestoes_2` FOREIGN KEY `FK_sugestoes_2` (`idsugestao1`)
REFERENCES `produtos` (`id`)
ON DELETE SET NULL
ON UPDATE CASCADE
, ROW_FORMAT = FIXED;
Does anyone have any idea what's going on?
Try this,
it works:
ALTER TABLE `sugestoes`
ADD CONSTRAINT `FK_idproduto_produtos_1` FOREIGN KEY (`idproduto`) REFERENCES `produtos` (`id`),
ADD CONSTRAINT `FK_sugestoes_produtos_2` FOREIGN KEY (`idsugestao1`) REFERENCES `produtos` (`id`),
ADD CONSTRAINT `FK_sugestoes_produtos_3` FOREIGN KEY (`idsugestao2`) REFERENCES `produtos` (`id`),
ADD CONSTRAINT `FK_sugestoes_produtos_4` FOREIGN KEY (`idsugestao3`) REFERENCES `produtos` (`id`),
ADD CONSTRAINT `FK_sugestoes_produtos_5` FOREIGN KEY (`idsugestao4`) REFERENCES `produtos` (`id`)
UPDATE:
You can not specify
ON DELETE SET NULL
Because of this:
You have defined a SET NULL condition though some of the
columns are defined as NOT NULL
You can see exact error when you run
SHOW ENGINE INNODB STATUS;
Perhaps removing the ROW_FORMAT = FIXED:
ALTER TABLE `infantile`.`sugestoes`
ADD CONSTRAINT `FK_sugestoes_2`
FOREIGN KEY `FK_sugestoes_2` (`idsugestao1`)
REFERENCES `produtos` (`id`)
ON DELETE SET NULL
ON UPDATE CASCADE
;
On second thought, how do you expect the ON DELETE SET NULL to behave when your column is defined with NOT NULL ?
And third, does the table has any data in it? If some of the rows violate this FK constraint, then you can't create that FK.