1
votes

When I create a migration for table at that time there is an issue with its through following error.

Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table yourwebs_veridocedu.#sql-2c46_8e (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table user_role_mappings add constraint user_role_mappings_user_id_foreign foreign key (user_id) references users (id) on delete cascade)

Migration for userrolemapping table:

public function up()
    {
        Schema::create('user_role_mappings', function (Blueprint $table) {
            $table->increments('id');

            $table->integer('user_id');
            $table->integer('group_id');
            $table->integer('roleid');
            $table->integer('status');
            $table->integer('createdby');
            $table->integer('modifiedby');
            $table->string('publicguid');
            $table->string('privateguid');
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('group_id')->references('id')->on('groups')->onDelete('cascade');
        });
    }

Migration for Role table :

public function up()
{
    Schema::create('userroles', function (Blueprint $table) {
        $table->increments('id');
        $table->string('rolename');
         $table->integer('status')->default(1);
        $table->integer('createdby')->default(1);
        $table->integer('modifiedby')->default(1);
        $table->string('publicguid');
        $table->string('privateguid');
        $table->timestamps(); 

    });
}

Usertable migration:

    <?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->increments('id');


            $table->string('name');
            $table->string('email',150)->unique();
            $table->string('password');
            $table->integer('roleid')->unsigned();
            $table->rememberToken();

            /*Common Fields*/
            $table->integer('status');
            $table->integer('createdby');
            $table->integer('modifiedby');
            $table->string('publicguid');
            $table->string('privateguid');
            $table->timestamps();

           /*From other table */
            $table->foreign('roleid') ->references('id')->on('userroles')->onDelete('cascade');

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */

    public function down()
    {
        Schema::dropIfExists('users');
    }
}

FK in table is not working which migrate.

3
Does user_role_mappings use the InnoDB engine? - Jonas Staudenmeir
No actually I have tried that one - Vasim Shaikh
It has to. Try $table->engine = 'InnoDB'; and $table->unsignedInteger('user_id');. - Jonas Staudenmeir
@JonasStaudenmeir It works!! - Vasim Shaikh

3 Answers

1
votes

increments columns are unsigned, your foreign key columns need to have the same signature. So you should change the columns like so:

$table->integer('user_id')->unsigned();
$table->integer('group_id')->unsigned();
1
votes

Try something as given below.

$table->integer('user_id')->unsigned()->index();
$table->integer('group_id')->unsigned()->index();
0
votes

Both tables have to use the InnoDB engine and the column types have to match:

Schema::create('user_role_mappings', function (Blueprint $table) {
    $table->engine = 'InnoDB';
    $table->unsignedInteger('user_id');
    $table->unsignedInteger('group_id');
});