MySql: Error Code: 1452 - mysql

Here is my erd diagram of my tables...
I am trying to INSERT VALUES into my items table using the following code...
INSERT INTO items (item, addedby, updated_at, created_at) VALUES ("one","two" NOW(), NOW())
I am getting the following error...
11:15:53 INSERT INTO items (item, addedby, updated_at, created_at) VALUES ("one", "two", NOW(), NOW()) Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`wishlist`.`items`, CONSTRAINT `fk_items_users` FOREIGN KEY (`users_id`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION) 0.046 sec
Whats going on!?

When you add relations in MySQL Workbench, it creates foreign key constraints automatically. Which means you have to provide a value for users_id which corresponds to an id in your users table.
If we assume you want to add an item for the user with id 1, your statement should look like this:
INSERT INTO items (item, addedby, updated_at, created_at, users_id) VALUES ("one","two" NOW(), NOW(), 1)
For further information look up referential integrity.
If you have just added the user and don't know his id because it's an AUTO_INCREMENT value, you can use LAST_INSERT_ID() to retrieve it.

Related

MySQL - Finding record that is leading to a duplicate entry in referenced table based on record number from error message

I have this query:
INSERT INTO `product`(`id`, `active`, `barcode`, `code`, `manufcode`, `manufname`, `name`, `taxid`, `unitname`, `pantheon_code_list`)
SELECT `id`, `active`, `barcode`, `code`, `manufcode`, `manufname`, `name`, `taxid`, `unitname`, `pantheon_code_list` FROM product_sync
ON DUPLICATE KEY UPDATE
id = product_sync.id,
active = product_sync.active,
barcode = product_sync.barcode,
manufcode = product_sync.manufcode,
manufname=product_sync.manufname,
name=product_sync.name,
taxid=product_sync.taxid,
unitname=product_sync.unitname,
pantheon_code_list=product_sync.pantheon_code_list
I have a product_tr table that has a foreign key on product(id) and when I run above query (in phpMyAdmin) I get this error:
#1761 - Foreign key constraint for table 'product', record '526' would lead to a duplicate entry in table 'product_tr', key 'PRIMARY'
Now the question is how to find which record it is based on record '526' from the error message?
Or is there any mechanism like query a hint in MySQL so I get more details about the row that is causing this constraint violation?

cannot add or update a child row error with temp table?

i keep getting the 'cannot add or update a child row' error and i cant seem to figure out what im doing wrong or how to fix the issue. the id field within all three tables have been set to unique and auto increment, so my thought was that if the 'slect * from(...)tbltmp' returns no value, than it would auto assign a new value upon insertion of the data.
INSERT INTO schedule (id, employee_id, project_id, project_phase, year_week, hours)
VALUES(
(SELECT * FROM(
SELECT schedule.id
FROM schedule
JOIN employees on employees.id = schedule.employee_id
WHERE employees.Employee=?
AND schedule.project_phase=?
AND schedule.year_week=?)
tbltmp),
(SELECT id
FROM employees
WHERE Employee=?),
(SELECT MIN(pID)
FROM projects
WHERE pNUM = ?
AND pPhase = ?),
?,
?,
?)
ON DUPLICATE KEY UPDATE hours=?'
this is what the error says
'Cannot add or update a child row: a foreign key constraint fails (`projectscheduler`.`schedule`, CONSTRAINT `employee_id` FOREIGN KEY (`id`) REFERENCES `employees` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)'
any help on this would be awesome.
You are looking for to update or add a row in a children table but the foreign key of children table was not found into the parent table.
Check the foreign keys in the parent table.

could anyone suggest me 2 solve this?

A Database Error Occurred
Error Number: 1452
Cannot add or update a child row: a foreign key constraint fails (dihr.tbl_contract, CONSTRAINT tbl_contract_ibfk_2 FOREIGN KEY (id) REFERENCES tbl_attactment (contract_id) ON DELETE CASCADE ON UPDATE CASCADE)
INSERT INTO tbl_contract (staff_id, type_id, start_date, end_date, status) VALUES ('2', '1', '2018-03-20', '2018-03-30', '1')
Filename: C:/xampp/htdocs/system/database/DB_driver.php
Line Number: 691
This is relationship table here
This is error message here
The id column in the table tbl_contract is dependent on the contract_id column in table tbl_attactment
You are not inserting this tbl_contract.id column in your insert.
If you do, you should be aware that the tbl_contract.id should exist in the tbl_attactment.contract_id

Cant figure out how to fix Error 1452

I cant figure out this error message.I know there are similar post but they have not helped.
ERROR 1452: Cannot add or update a child row: a foreign key constraint
fails (workingmodel.cadet, CONSTRAINT fk_Cadet_YEAR_t1 FOREIGN
KEY (YEAR_t_idYEAR_t) REFERENCES year_t (idYEAR_t) ON DELETE NO
ACTION ON UPDATE NO ACTION)
SQL Statement:
INSERT INTO `workingmodel`.`cadet` (`CID`, `YEAR_t_idYEAR_t`, `Rank_idRank`, `BN_idBN`, `PTscore`, `Academic_idAcademic`, `CadetLastName`, `CadetFirstName`) VALUES ('00123456', '2015', 'CPL', '2', '220', 'CS', 'Matthews', 'Bob')
It is all about Referential Integrity.
Table cadet is dependent on table year_t on column YEAR_t_idYEAR_t into column idYEAR_t.
You are inserting a value 2015 on table cadet that the value of YEAR_t_idYEAR_t doesn't exist on table year_t.
To fix your problem, you need to insert 2015 on table year_t first and execute your INSERT statement again.

mysql - what does this error mean?

I do not require this error to be solved. I just need to understand what it means so I can approach it myself.
Cannot add or update a child row: a foreign key constraint fails
(`db`.`transaction`, CONSTRAINT `transaction_ibfk_2`
FOREIGN KEY (`product_id`) REFERENCES `product` (`id`))
INSERT INTO `transaction` ( `client_id`, `cost`, `website_id`, `product_id`,
`template_number`, `value`, `order_id` )
VALUES ( '17', '', '2480', '', '', '12', '1');
What is a foreign key? How is it set?
What does CONSTRAINT transaction_ibfk_2 mean?
Does this mean I need to have a table called transaction_ibfk_2?
Thanks.
You are inserting an empty string as productid (Fourth item in the list)
INSERT INTO transaction
(client_id, cost, website_id, product_id, template_number, value, order_id)
VALUES ('17', '', '2480', '', '', '12', '1')
There is a referential integrity constraint set up to ensure that you must only insert productids matching one in the referenced product table.
This means you are trying to add a value into the foreign key column which is not available in the referenced column or trying to add blank in foreign key column.
i.e. You are trying to add product_id as blank which is not allowed. All values in foreign key column must be valid values present in the main referenced column id.
There's not necessarily a relationship between constraint names and tables although most people name them appropriately to make their lives easier.
All this means is that you have a transaction_ibfk_2 constraint. The actual problem is in the rest of the message:
FOREIGN KEY (product_id) REFERENCES product (id)
You need to insert a row into your product table first. The id that you insert there should be the product_id you're trying to insert into transaction (which is '' for some reason - I'm pretty certain this should be a real value (or possibly NULL)).
You can create foreign keys with a clause of the create table or alter table DDL statements.