I am trying to delete a particular record but I get the following error message - laravel-5.4

Illuminate \ Database \ QueryException (23000)
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (project-management.projects, CONSTRAINT projects_company_id_foreign FOREIGN KEY (company_id) REFERENCES companies (id)) (SQL: delete from companies where id = 2)

You can't delete the record that have child relation ships.
In companies table record #2 is used as projects_company_id of projects table.
So, First delete the projects with company_id #2 and delete company with id #2.
Or
you can use ON DELETE CASCADE option for that constraint(like auto delete, Child records will automatically deletes when parent record deleted).

Related

how i can update parent row (foreign key)

in mysql database i have tow tabel , between them relationship "foreign key"
when i want need update row but : show this missage
#1451 - Cannot delete or update a parent row: a foreign key constraint fails
(paymesomething.advertisers, CONSTRAINT advertisers_ibfk_1 FOREIGN KEY
(advertiser_id) REFERENCES jobs (advertiser_id))
You're trying to update jobs.advertiser_id, or delete a row in jobs, but there's at least one row in paymesomething that has an advertisers column that references it as a foreign key. The update would invalidate the relationship, so it's not allowed.
You can add ON DELETE CASCADE ON UPDATE CASCADE to the foreign key declaration in paymesomething. Then when you change the parent table, it will automatically delete or update the related rows in paymesomething.

how to make a one to one relationship in phpmyadmin?

I have two tables "donor" and " location", every donor has one location in a time.
how to make the keys for this relation?
I tried to make a foreign key in location table to the donor but it gives me this message:
Error
SQL query:
ALTER TABLE `location` ADD CONSTRAINT `location_donor` FOREIGN KEY (`donor_id`) REFERENCES `blood_donation`.`donor`(`id`) ON DELETE RESTRICT ON UPDATE RESTRICT;
MySQL said: Documentation
#1452 - Cannot add or update a child row: a foreign key constraint fails (`blood_donation`.`#sql-23f8_2e`, CONSTRAINT `location_donor` FOREIGN KEY (`donor_id`) REFERENCES `donor` (`id`))
If you want to execute ALTER TABLE statment you should truncate the table in the first place.
Because Mysql can not add the constraint to the existing rows according to the error log :
#1452 - Cannot add or update a child row: a foreign key constraint fails

MySQL delete row foreign key on multitable?

I have a big problem.
I need delete all row on multitable...on this model DB
http://i62.tinypic.com/m7fhap.png
Because when I try (DELETE FROM alergia WHERE Grupo = 'Alergia1') return this error
ERROR 1451: 1451: Cannot delete or update a parent row: a foreign key constraint fails (`odontologia`.`alergico`, CONSTRAINT `fk_Personas_has_Alergia_Alergia1` FOREIGN KEY (`Alergia_ID`) REFERENCES `alergia` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION)
SQL Statement:
DELETE FROM `odontologia`.`alergia` WHERE `ID`='2'"
I can do the deletion, but using some variables and sql statements.
I was reading and talking about DELETE CASCADE but I can not.
I need to delete any records in tables that have a direct or indirect relationship with the ID ALERGIAGRUPO ... thank you very much for the help
You need to do a join with Alergico and delete corresponding foreign key rows first
In case you have multiple such foreign key relations ships, you have to delete it from all such tables.
DELETE FROM Alergico
WHERE Alergia_ID in ( SELECT ID FROM Alergia where ID= 2)
Then you can do the deletion from Alergia
DELETE FROM Alergia where ID =2

error code #1452 when adding foreign keys

Error
SQL query:
ALTER TABLE `bids` ADD FOREIGN KEY (`buyerID`) REFERENCES `e_trading_post`.`buyer`(`buyerID`)
ON DELETE CASCADE ON UPDATE CASCADE;
MySQL said:
1452 - Cannot add or update a child row: a foreign key constraint fails ('e_trading_post' .'#sql-15d48_6a8', CONSTRAINT `#sql-15d48_6a8_ibfk_1' FOREIGN KEY ('buyerID') REFERENCES 'buyer; ('buyerID') ON DELETE CASCADE ON UPDATE CASCADE)
This error means that this specific foreign key constraint cannot be created as it would be violated (is is violated right now).
The following query might help you to find the violating row:
select buyerID from bids where buyerID not in (select buyer from e_trading_post)

SQL error code #1452

I am trying to create foreign key for my database. My database is for a gym system.
I have the table called user. Within that table I have user_id which is my primary key.
I have another table call member within this table I also have user_id which I want to make a foreign key so it relates to my user_id within the user table.
But i keep getting the following error..
#1452 - Cannot add or update a child row: a foreign key constraint fails (Gym_System.#sql-247a_3c1, CONSTRAINT #sql-247a_3c1_ibfk_1 FOREIGN KEY (user_id) REFERENCES members (user_id))
Can anyone help?