Laravel - Can't solve Integrity constraint violation - mysql

I have created a new migration to add a column to an existing table and add a foreign key to an existing table.
This is the migration with the new column:
Schema::table( 'events', function ( Blueprint $table ) {
$table->integer( 'category_id' )->unsigned()->after( 'place_id' );
$table->foreign('category_id')->references('id')->on('categories');
} );
When I run the migrate command I get:
[Illuminate\Database\QueryException]
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`meetmount`.`#sql-3c8_424`, CONSTRAINT `events_catego
ry_id_foreign` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`)) (SQL: alter table `events` add constraint events_category_id_foreign foreign key (`category_id`) r
eferences `categories` (`id`))
and
[PDOException]
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`meetmount`.`#sql-3c8_424`, CONSTRAINT `events_catego
ry_id_foreign` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`))
How can I solve?

The cause of the error could be the following:
The parent table categories and/or the child table events already have records in them. As you want to reference the parent table's particular column (id), MySQL will expect the child table to have a value that exists in the parent, hence the integrity constraint violation.
A possible solution (in case the parent is not empty) is to add a default value in the events table for the foreign key column:
$table->integer( 'category_id' )->unsigned()->after( 'place_id' );
$table->foreign('category_id')->references('id')->on('categories')->default(1);

Happened the same with me, but I resolved this adding a default value on creation of column:
$table->integer( 'category_id' )->unsigned()->after( 'place_id' )->default(1);
Make sure the table "categories" has a record with id = 1.

Related

Cannot add or update a child row: a foreign key constraint fails while adding another foreign key

I am trying to add a foreign key constraint in my table. My table structure are:
table requisition
My next table where i want to add foreign key of requisition table
requisition_approval
When i try to add a foreign key constraint with following sql query:
ALTER TABLE `requisition_approval` ADD CONSTRAINT `requisition_id` FOREIGN KEY (`requisition_id`) REFERENCES `requisition`(`id`) ON DELETE RESTRICT ON UPDATE RESTRICT;
It gives following error:
#1452 - Cannot add or update a child row: a foreign key constraint fails (`proprompt`.`#sql-34e8_3d7`, CONSTRAINT `requisition_id` FOREIGN KEY (`requisition_id`) REFERENCES `requisition` (`id`))
What is the problem here and how can i solve it?
Error is in the requisition_approval table more specificly in the requisition_id column. You inserted id 0 which does not references any id in requisition table. Change the value to 1 and this will work :)

Foreign key constraint fails in an empty table

I have a new project running, where there are 2 tables groups and members. They are both empty at the time and there is a foreing key constraint on members table group_id.
Now I start off by creating a group which may later have many members, however, as I try to create it, I receive the following message:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`cogro`.`groups`, CONSTRAINT `groups_ibfk_1` FOREIGN KEY (`id`) REFERENCES `members` (`group_id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
Any help or guidance is much appreciated.
You have entered wrong direction of the foreign key. Currently groups.id references members.group_id (in order to have an entry in groups with id = 1 you should have an entry in members with group_id = 1)
Change the defintion to:
CONSTRAINT `members_group_id_fk` FOREIGN KEY (`group_id`) REFERENCES `groups` (`id`)

SQL FK constraint fails

I have 2 tables, Office and User. I want to make relation OneToMany (1 office has many users).
But when i run this sql
ALTER TABLE izo_user ADD CONSTRAINT FK_DA8075CFFA0C224 FOREIGN KEY (office_id) REFERENCES izo_office (id)
CREATE INDEX IDX_DA8075CFFA0C224 ON izo_user (office_id)
something goes wrong and i get error
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`izoplast`.`#sql-9842_1be9`, CONSTRAINT `FK_DA8075CFFA0C224` FOREIGN KEY (`office_id`) REFERENCES `izo_office` (`id`))
My Tables: http://oi57.tinypic.com/whhezr.jpg
try it if it works for you-
ALTER TABLE izo_user add index idx_office_id(office_id);
ALTER TABLE izo_user ADD CONSTRAINT FK_DA8075CFFA0C224 FOREIGN KEY (office_id) REFERENCES izo_office (id);

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

Error 1215: cannot add foreign key constraint in my SQL

I have the parent table which is gym_member, and I have the child which is medical_history.
In gym_member table there are two primary key which are gm_id and student_id.
In medical history table there are one primary which is mh_id, and I want to add a foreign key which is student_id but it show me this error.
ALTER TABLE `hct_gym`.`medical_history`
ADD CONSTRAINT `student_id`
FOREIGN KEY (`student_id`)
REFERENCES `hct_gym`.`gym_member` (`student_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1215: Cannot add foreign key constraint
SQL Statement:
ALTER TABLE `hct_gym`.`medical_history`
ADD CONSTRAINT `student_id`
FOREIGN KEY (`student_id`)
REFERENCES `hct_gym`.`gym_member` (`student_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
I want to know where is the problem?
Check if column medical_history.student_id contains values
which are not contained in gym_member.student_id
That would contradict the constraint before /while it is being created.