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
Related
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'm trying to link all my TicketMethod objects to all the Event objects:
foreach (Event::all() as $event) {
$event->ticketMethods()->attach([1, 2]);
}
The 1 and 2 are the only ids in the db for TicketMethod.
In my Event I have the following code:
public function ticketMethods()
{
return $this->belongsToMany('App\Models\TicketMethod', 'event_ticket_methods');
}
Exception:
[Illuminate\Database\QueryException]
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update
a child row: a foreign key constraint fails (`tickets`.`event_ticket_method
s`, CONSTRAINT `event_ticket_methods_event_id_foreign` FOREIGN KEY (`event_
id`) REFERENCES `events` (`id`) ON DELETE CASCADE) (SQL: insert into `event
_ticket_methods` (`event_id`, `ticket_method_id`) values (#/Y�� ���ʫB{�1�,
1), (#/Y�� ���ʫB{�1�, 2))
The event_id fields are in binary format, which causes the question marks.
I don't get the error, as the event exists as I just retrieved them...
Ended up by checking all my migrations. The event_id column got a different length than the id field in the events table.
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 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
I'm almost sure its a silly problem but I am unable to see it. I have read through dozen of sites and tutorials with not hope. I have simple migration that creates a new table and defines it. I am trying to add a foreign key constraint to it but I get seemingly contradicting errors.
class CreateAlchemyPages extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
//
Schema::dropIfExists('alchemy_pages');
Schema::create('alchemy_pages', function(Blueprint $nt)
{
$nt->engine = 'InnoDB';
$nt->increments('id');
$nt->integer('course_id');
$nt->foreign('course_id')->references('ID')->on('Courses')->onDelete('cascade')->onUpdate('cascade');
$nt->integer('parent_page_id')->unsigned();
$nt->integer('navigation_order');
$nt->string('full_title');
$nt->string('short_title');
$nt->string('page_info');
$nt->integer('state');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
// Cleanup our tables
}
}
After running migrate, I get the following error:
[Illuminate\Database\QueryException]
SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write; duplicate key in table '#sql-60d4_d54' (SQL: alter t able
alchemy_pages add constraint alchemy_pages_course_id_foreign foreign
key (course_id) references Courses (ID) on delete cascade on
update cascade)
If I change it to this
//$nt->integer('course_id')->unsigned();
$nt->foreign('course_id')->references('ID')->on('Courses')->onDelete('cascade')->onUpdate('cascade');
It gives me the error:
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'course_id' doesn't exist in table (SQL: alter table
alchemy_pages add constraint alchemy_pages_course_id_foreign foreign
key (course_id) references Courses (ID) o n delete cascade on
update cascade)
What am I missing?