When I insert thing in my table via phpmyadmin I have no problem, but when I try to do it in the form I have created as "admin-panel" in my site i get this message:
Error: Cannot add or update a child row: a foreign key constraint fails (`db467610239`.`articulo`, CONSTRAINT `fk_articulo_genero` FOREIGN KEY (`genero_id`) REFERENCES `genero` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
Thing is the number I insert into my new table already exists in my other table as they are linked as genre. I don't know why this is happening, i'm using exactly the same insert as in phpmyadmin just with some php tweaks:
INSERT INTO articulo VALUES (id= null , nombre='$nombre', imagen='$imagen', text='$text', precio='$precio', popup='$popup', genero_id ='$genero_id')
Your query should look more like this:
$query = "
INSERT INTO articulo (
nombre, imagen, text, precio, popup, genero_id
) VALUES (
'$nombre', '$imagen', '$text', '$precio', '$popup', $genero_id
)";
Related
what is wrong with this query:
INSERT INTO `customers`(`username`, `password`)
VALUES (:username, :password)
WHERE NOT EXISTS (
SELECT `username` FROM `customers` WHERE `username`=:username
);
What's wrong with the query? It generates a syntax error.
If you want to prevent duplicates, then use a unique constraint on the table:
ALTER TABLE customers ADD CONSTRAINT unq_customers_username UNIQUE (username);
Then if you attempt to insert a duplicate value using
INSERT INTO `customers`(`username`, `password`)
VALUES (:username, :password);
You will get an error of a constraint violation.
Often in this case, you want to actually update the password in the existing row. You can do that using the MySQL extension ON DUPLICATE KEY UPDATE:
INSERT INTO `customers`(`username`, `password`)
VALUES (:username, :password)
ON DUPLICATE KEY UPDATE password = :password;
I am having trouble with the following insert query.
INSERT INTO CM_LABEL_CALENDAR (
label_id,
label_name,
order_seq,
meal_id,
hyperlink
)
SELECT
label_id,
label_name,
order_seq,
(meal_id + 315),
hyperlink
FROM
CM_LABEL_CALENDAR
WHERE
(meal_id BETWEEN '1466' AND '1521');
When I try to execute it I get the following error:
Lookup Error - MySQL Database Error: Cannot add or update a child row: a foreign key constraint fails (TEST_PBMS.CM_LABEL_CALENDAR, CONSTRAINT CM_LABEL_CALENDAR_ibfk_1 FOREIGN KEY (meal_id) REFERENCES CM_MEAL_CALENDAR (meal_id))
I've tried looking for an answer but couldn't find one.
There is a foreign key constraint between CM_LABEL_CALENDAR(meal_id) and CM_MEAL_CALENDAR(meal_id)
You are getting this error because you are trying to insert values in the meal_id column that do not exist in the CM_MEAL_CALENDAR table.
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.
I have this error when inserting values into an association table during transaction:
Cannot add or update a child row: a foreign key constraint fails (dev.genre, CONSTRAINT fk_Genre_EntityType1 FOREIGN KEY (idEntityType) REFERENCES entitytype (idEntityType) ON DELETE NO ACTION ON UPDATE NO ACTION)
Here is the part of the schema that describes the tables used:
and here is the create statement of the genre table:
-- -----------------------------------------------------
-- Table `dev`.`Genre`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `dev`.`Genre` (
`idGenre` INT NOT NULL ,
`Name` VARCHAR(45) NULL COMMENT 'musique, spectacle, expo' ,
`idEntityType` INT NOT NULL ,
`idStyle` INT NOT NULL ,
PRIMARY KEY (`idGenre`, `idEntityType`, `idStyle`) ,
INDEX `fk_Genre_EntityType1_idx` (`idEntityType` ASC) ,
INDEX `fk_Genre_Style1_idx` (`idStyle` ASC) ,
CONSTRAINT `fk_Genre_EntityType1`
FOREIGN KEY (`idEntityType` )
REFERENCES `dev`.`EntityType` (`idEntityType` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Genre_Style1`
FOREIGN KEY (`idStyle` )
REFERENCES `dev`.`Style` (`idStyle` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
To resume, the Genre tables references EntityType and Style, that's all.
The error occurs when I try to create a new Style and then add an association in the Genre table.
Everything is within a transaction, and what I do is:
create the new style in the Style table
get the id of the created style
insert an association in the genre table, and that's when I get the error.
I've searched quite a while on the web, but the only thing I found was this SO post: In MySQL, can I defer referential integrity checks until commit
I'm not sure this is what it is about here, because the error happens on a table that hadn't changed during the transaction (EntityType). Or am I missing something?
Can someone explain me the reason why I have this error please? (I'm stuck here)
Also, if it really have something to do with the SO post I mentioned earlier, is there a "clean" way of doing that kind of inserts without writing my own rollback mechanism?
Thanks for your answers
EDIT
the first query to insert a new style is:
CREATE PROCEDURE `Entity_CreateStyle`
(
IN p_name varchar(45),
OUT op_idStyle int(11)
)
BEGIN
insert into Style(idParentStyle, Name, IsValidated)
values(null, p_name, 0);
SET op_idStyle = LAST_INSERT_ID();
END
the next one, that produces the error:
CREATE PROCEDURE `Entity_AssociateStyleWithEntityType`
(
IN p_idGenre int(11),
IN p_Name varchar(45),
IN p_idEntityType int(11),
IN p_idStyle int(11)
)
BEGIN
insert into Genre(idGenre, Name, idEntityType, idStyle)
values(p_idGenre, p_Name, p_idEntityType, p_idStyle);
END
both are actually stored procedures that we call from C# using MySQL.Net connector
the p_idEntityType comes from the model (MVC) and I checked it's value is correct and exists in EntityType table.
The error message is clear: entitytype is referenced by genre. This means that ALL rows in entitytype must have a match in genre, or can't be inserted. There is a match when genre.entitytype = entitytype.entitytype.
I'm trying to use ON CASCADE DELETE in mysql db but I can't make it work.
Here is my code:
CREATE TABLE sometable
(
testId CHAR(43),
blocked BOOL,
PRIMARY KEY(testId)
);
CREATE TABLE p
(
testId CHAR(43),
phrase text,
source text,
FOREIGN KEY (testId) REFERENCES sometable (testId) on delete cascade
);
CREATE TRIGGER sometable_insert BEFORE INSERT ON `sometable` FOR EACH ROW SET NEW.`testId` =UUID();
I then perform an insert into sometable, which will generate a UUID.
I take this UUID and insert it to table p.
insert into p(testId, phrase, source) values('07616f60-424f-11df-871a-b98e9', 'fun', 'test');
When doing a delete on the row in sometable nothing happens in table p.
What have i missed or what I'm i doing wrong
Ok, found the solution. I had to specify the type to innodb like this:
CREATE TABLE sometable
(
testId CHAR(43),
blocked BOOL,
PRIMARY KEY(testId)
) type=innodb;
Thank you for the comments
// Jakob