vendredi 6 septembre 2019

How to solve this Laravel Error (errno: 150 "Foreign key constraint is incorrectly formed")?

I want to create foreign key on my migrate but it shows this error when I run "php artisan migrate" command -

Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1005 Can't create table `lr_agnes`.`#sql-2e8c_93` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `prices` add constraint `prices_condition_id_foreign` foreign key (`condition_id`) references `conditions` (`id`) on delete cascade on update cascade)

I already tried this things - 1. Drop all table from database including migration table.then I run "php artisan migrate" 2. Run "php artisan migrate:reset" 3. Run "php artisan migrate:refresh" 4. Run "php artisan migrate:fresh" There's no options didn't work. I tried to solve the problem by these things.

My Migration File Code -

 public function up()
    {
        Schema::create('prices', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->float('price');
            $table->integer('condition_id')->unsigned();
            $table->foreign('condition_id')
                ->references('id')
                ->on('conditions')
                ->onDelete('cascade')
                ->onUpdate('cascade');
            $table->timestamps();
        });
    }

when I run "php artisan migrate" Every time I got this error -

Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1005 Can't create table `lr_agnes`.`#sql-2e8c_93` (errno: 150 "Foreign key constraint is incorrectly formed") (SQ
L: alter table `prices` add constraint `prices_condition_id_foreign` foreign key (`condition_id`) references `conditions` (`id`) on delete cascade on update cascade)`enter code here`



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire