Duplicate Key issue after lost connection even thou constraint does not exist - mysql

I was doing a migration on laravel for all my DB's (several clients). All DB are the same structure and one of them lost connection and failed before completion. I tried to migrate again and I ran into an issue with a duplicate foreign key.
After looking at the constraints
SELECT * FROM information_schema.KEY_COLUMN_USAGE ke where constraint_name = 'rt_eqmtfixedcycle_excav_id_foreign'
SQL result for schema
All other databases successfully inserted 'rt_eqmtfixedcycle_excav_id_foreign' key. But the one that failed 'smv_Forestalleonera' database did not appear, but.
If I try to insert again:
use smv_forestalleonera;
alter table `rt_eqmtfixedcycle` add constraint `rt_eqmtfixedcycle_excav_id_foreign` foreign key (`excav_id`) references `rt_truck` (`id`) on delete restrict on update cascade
I get the following error:
12:33:45 alter table `rt_eqmtfixedcycle`
add constraint `rt_eqmtfixedcycle_excav_id_foreign`
foreign key (`excav_id`)
references `rt_truck` (`id`)
on delete restrict
on update cascade Error Code: 1005. Can't create table `smv_forestalleonera`.`#sql-7f4d_a8f7` (errno: 121 "Duplicate key on write or update") 0.234 sec
I'm using MariaDB 10.2.7.
I've already removed the table and restarted the engine without any results.
(from comment)
CREATE TABLE rt_eqmtfixedcycle (
id smallint(5) unsigned NOT NULL,
excav_id smallint(5) unsigned DEFAULT NULL,
locload_id int(10) unsigned DEFAULT NULL,
locdump_id int(10) unsigned DEFAULT NULL,
updated_at datetime DEFAULT NULL,
updated_by int(10) unsigned DEFAULT NULL,
KEY rt_eqmtfixedcycle_id_foreign (id),
CONSTRAINT rt_eqmtfixedcycle_id_foreign
FOREIGN KEY (id) REFERENCES rt_truck (id) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Related

MYSQL Error - Duplicate key on write or update

CREATE TABLE IF NOT EXISTS `demare`.`shop` (
`shop_id` INT NOT NULL AUTO_INCREMENT,
`shop_image` VARCHAR(255) NOT NULL,
`shop_price` FLOAT(6,2) NOT NULL,
`cart_id` INT NOT NULL,
PRIMARY KEY (`shop_id`),
INDEX `cart_id_idx` (`cart_id` ASC),
CONSTRAINT `cart_id`
FOREIGN KEY (`cart_id`)
REFERENCES `demare`.`shopping cart` (`cart_id`)
ON DELETE CASCADE
ON UPDATE NO ACTION)
ENGINE = InnoDB;
I have an error. My 'shop' table cannot be created as I have error:
121 Duplicate key on write or update.
I have provided the codes. Can anyone tell me what the problem is?
Problem exist because the foriegn key exist in memory, if possible drop the database and start again.

Error while creating a table in mysql cluster

I'm trying to create a table in MySQL Cluster . but getting a error while executing below query.
CREATE TABLE `tbl_anal_results` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`tbl_bill_bill_id` int(10) unsigned NOT NULL,
`item` varchar(256) NOT NULL,
`bill_value` decimal(10,2) NOT NULL,
`calc_value` decimal(10,2) NOT NULL,
`err` tinyint(1) NOT NULL,
`error_code` varchar(256) DEFAULT NULL,
`tbl_bill_info_id` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_tbl_anal_results_tbl_bill1_idx` (`tbl_bill_bill_id`),
CONSTRAINT `fk_tbl_anal_results_tbl_bill1` FOREIGN KEY (`tbl_bill_bill_id`) REFERENCES `tbl_bill` (`bill_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=ndbcluster AUTO_INCREMENT=16570225 DEFAULT CHARSET=latin1;
ERROR 1215 (HY000): Cannot add foreign key constraint
I have used same query which I used to create same table in old mysql setup.
and I have changed ENGINE as ENGINE=ndbcluster instead of ENGINE=InnoDB
also noticed UPDATE CASCADE not working in mysql cluster.
Cut from docs:
ON UPDATE CASCADE is not supported when the reference is to the parent table's primary key.
This is because an update of a primary key is implemented as a delete of the old row (containing the old primary key) plus an insert of the new row (with a new primary key). This is not visible to the NDB kernel, which views these two rows as being the same, and thus has no way of knowing that this update should be cascaded.

How can I reference a Foreign Key from db1 in db2 using MySQL

I have two MySQL databases: master and slave. The master db contains the memberid field. How can I reference memberid in the slave db? This is what I've tried using the slave db with no success:
DROP TABLE IF EXISTS `attend_date_temp`;
CREATE TABLE `attend_date_temp` (
`clientid` int(10) unsigned NOT NULL,
`groupid` int(10) unsigned NOT NULL,
`memberid` int(10) unsigned NOT NULL,
KEY `clientid` (`clientid`),
KEY `memberid` (`memberid`),
CONSTRAINT `attend_date_temp_ibfk_1` FOREIGN KEY (`clientid`) REFERENCES `clients` (`clientid`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `attend_date_temp_ibfk_2` FOREIGN KEY (`memberid`) REFERENCES `master.members` (`memberid`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8
When I view the memberid foreign key in that table, I get this message:
Table 'slave.master.members' doesn't exist.
Can someone tell me what I'm doing wrong? This is the reference I used.
Thanks.
REFERENCES `master.members`
should be:
REFERENCES `master`.`members`
Otherwise, it tries to find a table called master.members in the slave database.

Attempting to add a foreign key to a table fails?

I have two tables -
CREATE TABLE `FOO` (
`user_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
/*Nothing to see here*/
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=380 DEFAULT CHARSET=latin1;
CREATE TABLE `BAR` (
`ID` int(11) unsigned NOT NULL,
`UserID` int(11) unsigned DEFAULT NULL,
PRIMARY KEY (`ID`),
KEY `VSK_UserID_Index` (`UserID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I am attempting create a foreign key constraint on BAR.UserID referencing users.user_id -
ALTER TABLE `FOO`.`BAR`
ADD CONSTRAINT `BAR_UserID_FKey`
FOREIGN KEY (`UserID`)
REFERENCES `FOO`.`users` (`user_id`)
ON DELETE CASCADE
ON UPDATE CASCADE;
I keep getting this error -
Operation failed: There was an error while applying the SQL script to the database.
Error 1452: Cannot add or update a child row: a foreign key constraint fails
Both of these tables have data in them - could this be the reason why this is happening, or is there something wrong with how the tables are being created?
Is there something I need to alter on one of these tables to make this work?
The data already in one of the tables (in particular, `FOO`.`BAR`, since that is the one you're adding a constraint to) is not consistent with the data in `FOO`.`users` (`user_id`).
You must ensure that the values un the `FOO`.`BAR`.`UserID` column all exist in `FOO`.`users` (`user_id`). There may be null values or other values that do not exist in the other column.

Mysql Query Error #1005 150

I have a query but its showing error #1005 and 150 in details. Some innodb
How to get it right?
CREATE TABLE `iars` ( `id` int(10) unsigned NOT NULL auto_increment,
`SessionId` int(10) unsigned NOT NULL,
`TeacherId` int(10) unsigned NOT NULL,
`courseid` int(4) unsigned NOT NULL,
`semester` int(4) unsigned NOT NULL,
`PaperId` int(4) unsigned NOT NULL,
`groupid` int(4) unsigned NOT NULL,
`Type` varchar(4) default 1,
PRIMARY KEY (`id`),
UNIQUE KEY `Constrint_Index`
(`SessionId`,`TeacherId`,`courseid`,`semester`,`PaperId`,`groupid`),
KEY `FK_tid` (`TeacherId`),
KEY `FK_gid` (`GroupId`),
KEY `FK_pid` (`PaperId`),
CONSTRAINT `FK_gid` FOREIGN KEY (`GroupId`) REFERENCES `groups` (`id`) ON DELETE
CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_pid` FOREIGN KEY (`PaperId`) REFERENCES `papers` (`p_id`) ON DELETE
CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_ssessionid` FOREIGN KEY (`SessionId`) REFERENCES `sessions` (`id`) ON
DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_tid` FOREIGN KEY (`TeacherId`) REFERENCES `teachers` (`id`) ON DELETE
CASCADE ON UPDATE CASCADE )
From the MySQL manual:
You cannot issue DROP TABLE for an InnoDB table that is referenced by a FOREIGN KEY constraint, unless you do SET foreign_key_checks = 0. When you drop a table, the constraints that were defined in its create statement are also dropped.
If you re-create a table that was dropped, it must have a definition that conforms to the foreign key constraints referencing it. It must have the right column names and types, and it must have indexes on the referenced keys, as stated earlier. If these are not satisfied, MySQL returns error number 1005 and refers to error 150 in the error message.
This means one of your other tables is probably referencing a column in iars which you are not re-creating. The solution, then, is to weed through all of your tables (via describe queries) and see where the reference is and either add the referenced column to your CREATE TABLE here, or remove the reference, as appropriate for your schema.
edit a very helpful gem from later on that referenced page:
SHOW ENGINE INNODB STATUS;
reveals loads of good info on the foreign-key error which just occurred. It should point you exactly where you need to look.