1

i have some table to migrate there are in followed. if i try to migrate that table in to the server database it show the General error: 1005. and i give the error and the server details on bottom. please help me to migrate with out error.

1.)users

Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });

2.)companies

Schema::create('companies', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->longText('description');
            $table->unsignedInteger('user_id');
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users');
        });

3.projects

Schema::create('projects', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->longText('description');
            $table->unsignedInteger('company_id');
            $table->unsignedInteger('user_id');
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users');
            $table->foreign('company_id')->references('id')->on('companies');
        });

server details

server: 127.0.0.1 via TCP/IP
Server type: MariaDB
Server connection: SSL is not being used Documentation
Server version: 10.1.38-MariaDB - mariadb.org binary distribution
Protocol version: 10
User: root@localhost
Server charset: UTF-8 Unicode (utf8)
DB Name: laravel

ERROR:

 Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1005 Can't create table `laravel`.`#sql-91c_30` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `companies` add constraint `companies_user_id_foreign` foreign key (`use
r_id`) references `users` (`id`))
kealaxshan
  • 338
  • 2
  • 14

1 Answers1

1

As I see, there is a problem of data type mismatch as you are creating PK of bigInteger and foreign keys of integer,

Schema::create('companies', function (Blueprint $table) {
...
$table->unsignedBigInteger('user_id');
});

And in other tables

Schema::create('projects', function (Blueprint $table) {
...
$table->unsignedBigInteger('company_id');
$table->unsignedBigInteger('user_id');
});

Do this and check again.

Rahul
  • 18,271
  • 7
  • 41
  • 60
  • The foreign keys are `unsignedInteger`. It's still a mismatch. – Barmar Apr 23 '19 at 06:27
  • That's my only concern. Foreign keys are `unsignedInteger`, it should be `unsignedBigInteger` as PK of foreign tables are `bigIntegers`. – Rahul Apr 23 '19 at 06:29