MySQL After Insert Trigger to table with FK to first table - mysql

This is my current Trigger:
CREATE DEFINER=`root`#`localhost` TRIGGER `setaccessrole` AFTER INSERT ON `user` FOR EACH ROW BEGIN
INSERT INTO user_role_linker (user_id, role_id) values (last_insert_id(), 2);
END
user_role_linker.user_id is FK to user.id, I want an insert into user_role_linker including the last inserted autoincremented ID + user_role = 2, but I get:
#1452 - Cannot add or update a child row: a foreign key constraint fails (`database`.`user_role_linker`, CONSTRAINT `FK_61117899A76ED395` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`))
edit:
I had to use "NEW.id" instead of last_insert_id().
This works.

edit: I had to use "NEW.id" instead of last_insert_id(). This works.

Related

Mysql on update cascade not working with tree table [duplicate]

So, I am getting:
Error Code: 1451. Cannot delete or update a parent row: a foreign key
constraint fails (playground.Person, CONSTRAINT sk_Person_Parent
FOREIGN KEY (parent_id) REFERENCES Person (id) ON DELETE CASCADE
ON UPDATE CASCADE)
This is the simple table, referencing itself:
CREATE TABLE IF NOT EXISTS Person (
id int not null primary key,
name varchar(100) not null,
parent_id int null,
CONSTRAINT `sk_Person_Parent`
FOREIGN KEY (parent_id)
REFERENCES Person (id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
as you see, there is "ON UPDATE CASCADE".
I insert 4 simple rows in it:
INSERT INTO Person(id, name, parent_id)
VALUES
(1, 'vasko', NULL),
(2, 'asdas', 1),
(3, 'ivo', 1),
(4, 'anton', 3);
so I have
1 - vasko
2 - asdasd
3 - ivo
4 - anton.
When i delete by id 1, all the records get wiped, because of the ON DELETE CASCADE. However, if I try to execute
UPDATE Person
SET id=10
WHERE id=1;
I get the given error. Any ideas?
(I am expecting vasil's id to become 10, and the parent_id of the next 2 rows to be updated to 10)
It is limitation in Mysql:
If ON UPDATE CASCADE or ON UPDATE SET NULL recurses to update the same table it has previously updated during the cascade, it acts like RESTRICT. This means that you cannot use self-referential ON UPDATE CASCADE or ON UPDATE SET NULL operations.
reference here

Cannot add or update a child row: a foreign key constraint fails when trying to add a new row

I'm currently trying to add a new entry in a table. In this table I've a constraint to my users table (the user id). In some cases my user id can be not set yet. In the case it's empty, I'm getting an error:
Cannot add or update a child row: a foreign key constraint fails
This is how I setup my constraint:
So is there a way to insert an empty value to a constraint field? If no, what would be the best solution instead of removing the constraint?
So is there a way to insert an empty value to a constraint field?
Yes, there is. As long as the column controlled by the foreign key is not defined as NOT NULL, you can insert a NULL value into it, as explain in the manual. What is not allowed is to insert a non-NULL value that does not exists in the parent table (this includes the empty string!).
MySQL essentially implements the semantics defined by MATCH SIMPLE, which permit a foreign key to be all or partially NULL. In that case, the (child table) row containing such a foreign key is permitted to be inserted, and does not match any row in the referenced (parent) table.
Consider this demo on DB Fiddlde:
-- parent table
create table parent (id int primary key);
-- child table
create table child (
id int primary key,
parent_id int null, -- allow `NULL` values
constraint parent_id_fk foreign key(parent_id) references parent(id)
);
-- create a parent record
insert into parent(id) values(1);
-- insert a child record that references the parent: ok
insert into child(id, parent_id) values(1, 1);
-- insert a child record with a NULL parent_id : ok
insert into child(id, parent_id) values(2, NULL);
-- insert a child record with a (non-NULL) unknown parent_id
insert into child(id, parent_id) values(3, 2);
-- Error: Cannot add or update a child row: a foreign key constraint fails

How to ignore foreign constraints and add NULL instead

I have two tables which share same primary/foreign key for ID:
Table1: PRIMARY KEY (id));
Table2: FOREIGN KEY (ident) REFERENCES table1(id);
I am trying to create a trigger in order to let me insert new row with non existing primary key and set on that field NULL but i am getting:
Cannot add or update a child row: a foreign key constraint fails error.
How can i solve this issue ?
this is what i tried but so far:
DELIMITER $$
CREATE TRIGGER t1 BEFORE INSERT on table1
FOR EACH ROW
BEGIN
IF NEW.ident!= (
SELECT ident
FROM dimos
WHERE ident NOT IN (
SELECT id
FROM table1
)
)
THEN
SET NEW.ident = 'NULL';
END IF;
END $$
DELIMITER ;
so i expect to get all field in a row expert the foreign key, foreign key must be null!

MySql "cannot update parent row" when I have "ON UPDATE CASCADE"

So, I am getting:
Error Code: 1451. Cannot delete or update a parent row: a foreign key
constraint fails (playground.Person, CONSTRAINT sk_Person_Parent
FOREIGN KEY (parent_id) REFERENCES Person (id) ON DELETE CASCADE
ON UPDATE CASCADE)
This is the simple table, referencing itself:
CREATE TABLE IF NOT EXISTS Person (
id int not null primary key,
name varchar(100) not null,
parent_id int null,
CONSTRAINT `sk_Person_Parent`
FOREIGN KEY (parent_id)
REFERENCES Person (id)
ON DELETE CASCADE
ON UPDATE CASCADE
);
as you see, there is "ON UPDATE CASCADE".
I insert 4 simple rows in it:
INSERT INTO Person(id, name, parent_id)
VALUES
(1, 'vasko', NULL),
(2, 'asdas', 1),
(3, 'ivo', 1),
(4, 'anton', 3);
so I have
1 - vasko
2 - asdasd
3 - ivo
4 - anton.
When i delete by id 1, all the records get wiped, because of the ON DELETE CASCADE. However, if I try to execute
UPDATE Person
SET id=10
WHERE id=1;
I get the given error. Any ideas?
(I am expecting vasil's id to become 10, and the parent_id of the next 2 rows to be updated to 10)
It is limitation in Mysql:
If ON UPDATE CASCADE or ON UPDATE SET NULL recurses to update the same table it has previously updated during the cascade, it acts like RESTRICT. This means that you cannot use self-referential ON UPDATE CASCADE or ON UPDATE SET NULL operations.
reference here

SQL: Auto-reference table on update cascade

I have a table with a foreign key auto-referenced, like this:
CREATE TABLE user
(
id INT,
name VARCHAR(20),
ref INT,
PRIMARY KEY(id)
)ENGINE=InnoDB;
ALTER TABLE user
ADD FOREIGN KEY (ref) REFERENCES user(id)
ON DELETE RESTRICT ON UPDATE CASCADE;
Then, I insert values in the table:
INSERT INTO user
VALUES(1, "User1", NULL), (2, "User2", 1), (3, "User3", 1), (4, "User4", 3);
SELECT * FROM user;
And I update a user to check if, when I update the id of a user, then the FK is updated on cascade:
UPDATE user
SET id = 5
WHERE id = 1;
But I get this error:
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails ('prueba'.'user', CONSTRAINT 'user_ibfk1' FOREIGN KEY ('ref') REFERENCES 'user' ('id') ON UPDATE CASCADE)
Can anyone say me what I'm doing wrong? Thank you.
see the dev document. Your self-referential "ON UPDATE CASCADE" acts like "ON UPDATE RESTRICT".
http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
If ON UPDATE CASCADE or ON UPDATE SET NULL recurses to update the same table it has previously updated during the cascade, it acts like RESTRICT. This means that you cannot use self-referential ON UPDATE CASCADE or ON UPDATE SET NULL operations. This is to prevent infinite loops resulting from cascaded updates. A self-referential ON DELETE SET NULL, on the other hand, is possible, as is a self-referential ON DELETE CASCADE. Cascading operations may not be nested more than 15 levels deep.