On Cascade Delete does not seem to be applying in my DB - mysql

I don't know what I am doing wrong as I've been looking at previous answers on this site concerning ON CASCADE DELETE.
Basically this is my table:
CREATE TABLE `directorycolumntags` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`directorycolumn_id` INT(11) NOT NULL,
`tag_id` INT(11) NOT NULL,
`description` TEXT,
`created` DATETIME DEFAULT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`directorycolumn_id`) REFERENCES directorycolumn(id),
CONSTRAINT FOREIGN KEY (`tag_id`) REFERENCES tag(id)
ON DELETE CASCADE
) ENGINE=MYISAM AUTO_INCREMENT=29 DEFAULT CHARSET=utf8;
The Foreign key references the id of the tag table:
CREATE TABLE `tag` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(200) DEFAULT NULL,
`description` TEXT,
PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8;
Now, If I perform this query to INSERT some data into the directorycolumntags table it works:
INSERT INTO directorycolumntags (directorycolumn_id, tag_id) VALUES (178,32);
However, when I DELETE the entry from the tag table with the id of 32 it does not remove the row from the directorycolumntags table. Can anyone point out where I am going wrong?

It's because your table directorycolumntags is MYISAM, not INNODB. MyISAM doesn't support foreign keys. You can write your foreign key statements, but MySQL silently ignores them.
Try this:
ALTER TABLE `directorycolumntags` ENGINE = 'InnoDB';

Related

mysqldump does not keep ON UPDATE RESTRICT

I have the following create-table-query:
`CREATE TABLE `order_meta` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`order_id` int(11) DEFAULT NULL,
`meta_key` varchar(255) DEFAULT NULL,
`meta_value` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `order_meta_key_index` (`order_id`,`meta_key`),
CONSTRAINT `order_meta_order_id` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB AUTO_INCREMENT=34064 DEFAULT CHARSET=utf8;`
when i run an export / mysqldump, it gives me. running SHOW CREATE TABLE order_meta gives me the same, so i guess thats where the problem lies.
`CREATE TABLE `order_meta` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`order_id` int(11) DEFAULT NULL,
`meta_key` varchar(255) DEFAULT NULL,
`meta_value` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `order_meta_key_index` (`order_id`,`meta_key`),
CONSTRAINT `order_meta_order_id` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=34064 DEFAULT CHARSET=utf8;`
notice the 'ON UPDATE RESTRICT' that has been removed. After when i delete a row from 'orders' the rows in 'order_meta' are not removed. When i remove the foreign key and re-add with the on update, it works again. Is this expected behavior or am i missing something here?
`alter table order_meta drop FOREIGN KEY `order_meta_order_id`;`
`alter table order_meta add CONSTRAINT `order_meta_order_id` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;`
I guess this had to do with outdated workbench to create the mysqldump.

Cannot add or update a child row: a foreign key constraint fails mysql

CREATE TABLE `class` (
`class_id` int(11) NOT NULL AUTO_INCREMENT,
`section_name` varchar(50) NOT NULL,
`class_alias` varchar(200) NOT NULL,
`grading_scheme` int(11) NOT NULL DEFAULT '0',
`year` year(4) NOT NULL,
`grade_calc_method_id` varchar(20) DEFAULT NULL,
PRIMARY KEY (`class_id`)
) ENGINE=InnoDB AUTO_INCREMENT=48819 DEFAULT CHARSET=latin1;
CREATE TABLE `teachers` (
`teacher_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`teacher_subject` varchar(20) NOT NULL DEFAULT 'None',
PRIMARY KEY (`teacher_id`),
KEY `user_id` (`user_id`,`school_id`)
) ENGINE=InnoDB AUTO_INCREMENT=48606 DEFAULT CHARSET=latin1;
CREATE TABLE `teacher_classes` (
`teacher_class_id` int(11) NOT NULL AUTO_INCREMENT,
`teacher_id` int(11) NOT NULL,
`class_id` int(11) NOT NULL,
PRIMARY KEY (`teacher_class_id`),
UNIQUE KEY `teacher_id_class_id` (`teacher_id`,`class_id`),
KEY `teacher_id` (`teacher_id`,`class_id`)
) ENGINE=InnoDB AUTO_INCREMENT=46707 DEFAULT CHARSET=latin1;
Trying to insure data consistency between the tables by using foreign key so that the DBMS can check for errors.I have another junction table teacher_classes
Here is my query to add foreign keys constraint
ALTER TABLE teacher_classes
ADD CONSTRAINT `tc_fk_class_id` FOREIGN KEY (`class_id`)
REFERENCES class (`class_id`) ON UPDATE NO ACTION ON DELETE NO ACTION,
ADD CONSTRAINT `tc_fk_teacher_id` FOREIGN KEY (`teacher_id`)
REFERENCES teachers (`teacher_id`) ON UPDATE NO ACTION ON DELETE NO ACTION;
've seen the other posts on this topic, but no luck, getting following error.
Cannot add or update a child row: a foreign key constraint fails
(DB_NAME.#sql-403_12, CONSTRAINT
tc_fk_teacher_id FOREIGN KEY (teacher_id) REFERENCES teachers
(teacher_id) ON DELETE NO ACTION ON UPDATE NO ACTION)
Too late to Answer. I just had the same problem the solution is easy.
You're getting this error because you're trying to or UPDATE a row to teacher_classes doesn't match the id in table teachers.
A simple solution is disable foreign key checks before performing any operation on the table.
SET FOREIGN_KEY_CHECKS = 0;
After you are done with the table enable it again.
SET FOREIGN_KEY_CHECKS = 1;
Or you can remove not null constraint and insert a NULL value in it.
That's most probably the column definition doesn't match properly. For table teachers the PK column definition is as below.
`teacher_id` int(11) NOT NULL AUTO_INCREMENT
Make sure you have the same definition in your child table teacher_classes

Cannot add or update a child row: a foreign key constraints. Which is not possible

I'm facing a really odd problem with MySQL, I got this following error when adding a new row :
Cannot add or update a child row: a foreign key constraint fails (gestikids_demo.child_moments, CONSTRAINT fk_child_moments_moment_16 FOREIGN KEY (moment_id) REFERENCES moments (id))
But I correctly add an existing moment_id in my entry. Here's the tables defintions :
CREATE TABLE `child_moments` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`child_id` bigint(20) DEFAULT NULL,
`moment_id` bigint(20) DEFAULT NULL,
`day` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uq_child_moments_1` (`child_id`,`moment_id`,`day`),
KEY `ix_child_moments_child_15` (`child_id`),
KEY `ix_child_moments_moment_16` (`moment_id`),
CONSTRAINT `fk_child_moments_child_15` FOREIGN KEY (`child_id`) REFERENCES `childs` (`id`),
CONSTRAINT `fk_child_moments_moment_16` FOREIGN KEY (`moment_id`) REFERENCES `moments` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=196596 DEFAULT CHARSET=latin1
CREATE TABLE `moments` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`is_meal` tinyint(1) DEFAULT '0',
`sort` int(11) DEFAULT NULL,
`pole_id` bigint(20) DEFAULT NULL,
`type_id` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `ix_moments_pole_72` (`pole_id`),
KEY `ix_moments_type_73` (`type_id`)
) ENGINE=MyISAM AUTO_INCREMENT=117 DEFAULT CHARSET=latin1
What is wrong ?
I tested to enter manually the data, and all the fields are populated with existing IDs, but the insert fail.
Thank you for your help.
Update: If I insert a data with no moment_id, the insert is successful. I checked if the foreign key is correctly written (no mistakes in the constraints) but it doesn't seems.
Update 2:
I tried recreating a simplified version of those two tables in an other database to see if the error was really about them, and I had the following error :
Can't create table 'child_moments' (errno: 150)
The insert was the following :
CREATE TABLE `child_moments` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`moment_id` bigint(20) DEFAULT NULL,
`day` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uq_child_moments_1` (`moment_id`,`day`),
KEY `ix_child_moments_moment_16` (`moment_id`),
CONSTRAINT `fk_child_moments_moment_16` FOREIGN KEY (`moment_id`) REFERENCES `moments` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `moments` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`is_meal` tinyint(1) DEFAULT '0',
`sort` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
) ENGINE=MyISAM DEFAULT CHARSET=latin1
And if I remove the following lines, the insert works successfully :
KEY `ix_child_moments_moment_16` (`moment_id`),
CONSTRAINT `fk_child_moments_moment_16` FOREIGN KEY (`moment_id`) REFERENCES `moments` (`id`)
So the problem is clearly located at these lines, but I can't explain what and why :/
I finally found the problem, thanks to MySQL Creating tables with Foreign Keys giving errno: 150
It's in fact really simple : the two tables does not have the same engine ! Yes, moments is MyISAM and child_moment is InnoDB.
Switching both to InnoDB fixed the issue for me.
Hope it'll helps others too!

Use some kind of inheritance with database tables

Is it possible (and how can I achieve it) to get some kind of inheritance with database tables?
I intend to have a parent table X
CREATE TABLE `X` (
`xId` int(11) NOT NULL AUTO_INCREMENT,
`addressId` int(11) NOT NULL,
`poiType` enum('ParkingPOI','ChargingPOI') NOT NULL,
`status` enum('AVAILABLE','NOT_AVAILABLE') NOT NULL
PRIMARY KEY (`aId`),
KEY `fk_x_address_idx` (`addressId`),
CONSTRAINT `fk_address` FOREIGN KEY (`addressId`) REFERENCES `addresses` (`addressId`)
ON DELETE NO ACTION ON UPDATE CASCADE) ENGINE=InnoDB AUTO_INCREMENT=217 DEFAULT CHARSET=utf8;
and a child table B
CREATE TABLE `B` (
`bId` int(11) NOT NULL AUTO_INCREMENT,
`xId` int(11) NOT NULL',
`status` enum('AVAILABLE','NOT_AVAILABLE','PARTLY_AVAILABLE') NOT NULL
PRIMARY KEY (`bId`),
KEY `aId` (`xId`),
CONSTRAINT `fk_a` FOREIGN KEY (`xId`) REFERENCES `X` (`xId`) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
In table X I want to have something like an ENUM with values being inherited from the child table, for instance: AVAILABLE, NOT_AVAILABLE. In this table/column of the parent table (X) only these values shall be allowed.
In the child table, however, this ENUM should be extendend so that the values AVAILABE, NOT_AVAILABLE, PARTLY_AVAILABLE shall be possible.
How can I achieve this?

MySQL Foreign Key Error

I want to create a database and connect it with two database. But I don't know when i create the Foreign Key to the second database (time_shift table) it always result error 150;
Here is the structure of table outlet:
And this is structure of table time_shift:
And this the query to create new table tide_cart:
create table `tide_chart` (
`id` int(10) not null auto_increment primary key,
`date` date null,
`outletId` int(11) not null,
`timeShiftId` int(11) not null,
`value` varchar(255) not null,
unique (`date`, `outletId`, `timeShiftId`),
foreign key (`outletId`) references `outlet`(`id`)
ON update cascade ON delete cascade,
foreign key (`timeShiftId`) references `time_shift`(`id`)
ON update cascade ON delete cascade
) engine=innoDB;
Please explain to me, why i get error when try to make foreign key connect to table time_shift?
Update: add dump export structure table for outlet and time_shift:
CREATE TABLE `outlet` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
CREATE TABLE `time_shift` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`time_start` time NOT NULL,
`time_end` time NOT NULL,
`is_active` tinyint(4) NOT NULL DEFAULT '1',
`ref_area` int(11) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
KEY `ref_area` (`ref_area`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;
You need to use the InnoDB engine for your tables. time_shift is using MyISAM.
You must define indexes for outletId and timeShiftId (either UNIQUE or KEY as needed) in order to be able to create a foreign key using that field.