I want to delete a record in database where it is a foreign key. The below code has been written for product table.
public function delete() {
$id = $this->request->params['pass'][0];
if( $this->Product->delete( $id )){
$this->Session->setFlash('Product was deleted');
$this->redirect(array('action'=>'index'));
} else {
$this->Session->setFlash('Unable to delete product');
$this->redirect(array('action' => 'index'));
}
}
Now what should I do to delete it from master table also?
This is the error I'm getting:
Error: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`indianautogas1`.`agent_to_consumer`, CONSTRAINT `agent_to_consumer1_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`))
SQL Query: DELETE `Product` FROM `indianautogas1`.`products` AS `Product` WHERE `Product`.`id` = 1
Related
I am getting this error:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or
update a child row: a foreign key constraint fails
(prj_tvto.activities, CONSTRAINT activities_school_id_foreign FOREIGN
KEY (school_id) REFERENCES schools (id)) (SQL: insert into activities
(school_id, cluster_id, group_id, updated_at, created_at) values (0,
2, 3, 2018-08-14 05:02:28, 2018-08-14 05:02:28))
In Controller
public function store(Request $request, SchoolsList $schoolsList)
{
if($request->ajax()) {
$activity = new Activity();
$activity->school_id = intval($request->school_id);
$activity->cluster_id = $request->cluster_id;
$activity->group_id = $request->group_id;
$activity->save();
return response()->json(['data_activity' => $request->all(), 'id' => $activity->id]);
}
}
And Ajax
var school_id = {school_id: data_school.id} ;
console.trace(school_id);
I am using laravel 5.4
I'm trying to implement a delete on my controller but then I have this error:
Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`greatsup_wz`.`entryitems`, CONSTRAINT `entryitems_fk_check_entry_id` FOREIGN KEY (`entry_id`) REFERENCES `entries` (`id`)) (SQL: delete from `greatsup_wz`.`entries` where `id` = 686)
Here is a piece of my code:
$entryTable = DB::table(config("app.DB_ACCOUNTING").".entryitems")
->where('entryref_id','=',100)->get();
foreach($entryTable as $entryTbl):
DB::table(config("app.DB_ACCOUNTING").".entries")
->where('id','=',$entryTbl->entry_id)->delete(); // this is where my error occurred
endforeach;
How can I fix it ?
The constraints that you have added will prevent data from being deleted. You need to update database schema. You need to add onDelete('cascade') in the entryitems migration
Schema::create('tablename', function (Blueprint $table) {
$table->integer('columnName')->unsigned();
$table->foreign('columnName')->references('columnName')->on('relatedTableName')->onDelete('cascade');
});
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.
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`)
ERROR 1452: 1452: Cannot add or update a child row: a foreign key constraint fails (`harris`.`MANAGER`, CONSTRAINT `fk_EMPLOYEE_has_PROJECT_PROJECT1` FOREIGN KEY (`PROJECT_project_ID`) REFERENCES `PROJECT` (`project_ID`) ON DELETE NO ACTION ON UPDATE NO ACTION)
SQL Statement:
INSERT INTO `harris`.`MANAGER` (`EMPLOYEE_employee_ID`, `PROJECT_project_ID`) VALUES ('EMPLOYEE_employee_ID', 'PROJECT_project_ID')
No matter what foreign key I use it won't work. For some reason it adds a "1" to the end IE: PROJECT_PROJECT when it should just be project_id and employye_num