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?
Related
I want to delete a faculty but I have this error
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (trouverm_m_ecole.etablissement_filieres, CONSTRAINT etablissement_filieres_id_filieres_foreign FOREIGN KEY (id_filieres) REFERENCES filieres (id)) (SQL: delete from filieres where id = 1)
My migration
Schema::create('etablissement_filieres', function (Blueprint $table) {
$table->id();
$table->bigInteger('id_etablissements')->unsigned();
$table->foreign('id_etablissements')->references('id')->on('etablissements');
$table->bigInteger('id_filieres')->unsigned();
$table->foreign('id_filieres')->references('id')->on('filieres');
$table->string('prise_en_charge');
$table->string('prix');
$table->string('prix_affecte')->nullable();
$table->timestamps();
});
my controller
public function destroy($id)
{
Filiere::destroy($id);
return redirect('/tableau-de-bord/admin/dashboard/sindhost/toutes-les-filieres');
}
Any ideas ?
The problem is, there is a etablissement_filieres record referencing a record of filieres. Unless you have specified what to do when deleting a record refered by another record as a foreign key, it does not allow you to delete the foreign key record. Ideal way to handle this situation is to specify what to do when deleting a foreign key record. Check the Foreign Key Constraints of Laravel documentation to see the available options. Look at the section where it shows how to define the actions such as,
->onUpdate('cascade')
->onDelete('cascade');
I'm addign a foreign key to my appointments table, my DB is called arl through laravel migrations
but when I run migration:
public function up()
{
Schema::table('appointments', function (Blueprint $table) {
$table->foreign('order_detail_id')->references('id')->on('order_details');
});
}
I got this error:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`arl`.`#sql-5470_6`, CONSTRAINT `appointments_order_detail_id_foreign` FOREIGN KEY (`order_detail_id`) REFERENCES `order_details` (`id`)) (SQL: alter table `appointments` add constraint `appointments_order_detail_id_foreign` foreign key (`order_detail_id`) references `order_details` (`id`))
checking the error I don't know why the sql doesn't recognize my table name and put this weird name arl`.`#sql-5470_6 should be arl`.`appointments
I went to heidi sql to replicate de query and I got the same error.
Edit: the problem it's on laragon local serve I tried with xampp and worked fine
You must delete all rows in table order_details and try again.
Or:
add ->default( any id from table order_details)
$table->unsignedBigInteger('order_detail_id')->default(1);
$table->foreign('order_detail_id')->references('id')->on('order_details');
try this :
$table->unsignedBigInteger('order_detail_id');
$table->foreign('order_detail_id')->references('id')->on('order_details');
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 am trying to create a pivot table, with foreign keys, this is the migration I have made in Laravel:
public function up()
{
Schema::create('player_position', function (Blueprint $table) {
$table->integer('player_id')->unsigned()->index();
$table->integer('position_id')->unsigned()->index();
$table->foreign('player_id')->references('id')->on('players')->onDelete('cascade');
$table->foreign('position_id')->references('id')->on('positions')->onDelete('cascade');
});
}
But, I get an error:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
(SQL: alter table player_position add constraint
player_position_posi tion_id_foreign foreign key (position_id)
references positions (id) on delete cascade)
[PDOException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
I have read that usually foreign key constraint errors are about not assigning unsigned to fields, or already having records in the DB, but my DB is empty, and I my fields have unsigned, so don't know what is the problem?
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
For a field to be defined as a foreign key, the referenced parent field must have an index defined on it. And the datatype of both the column and its size must be the same.
I think you are violating some of the above rule.
Remove ->index() method as it creates basic index while you want to add foreign key constraint which references primary key on another table.
$table->integer('player_id')->unsigned();
$table->integer('position_id')->unsigned();
$table->foreign('player_id')->references('id')->on('players')->onDelete('cascade');
$table->foreign('position_id')->references('id')->on('positions')->onDelete('cascade');