Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

neba0317's avatar

Laravel migration works on windows but not working in linux

when I run "php artisan migrate:fresh --seed" command it works on windows wamp server

but run this code on linux it gives me this error

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `users` add constraint `users_user_type_foreign` foreign key (`user_type`) references `user_type` (`id`) on delete restrict)

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:685
    681▕         // If an exception occurs when attempting to run a query, we'll format the error
    682▕         // message to include the bindings with SQL, which will make this exception a
    683▕         // lot more helpful to the developer instead of just the database's errors.
    684▕         catch (Exception $e) {
  ➜ 685▕             throw new QueryException(
    686▕                 $query, $this->prepareBindings($bindings), $e
    687▕             );
    688▕         }
    689▕

      +9 vendor frames
  10  database/migrations/2014_10_12_000000_create_users_table.php:35
      Illuminate\Support\Facades\Facade::__callStatic()

      +32 vendor frames
  43  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

this is my user migration

<?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('username')->unique();
            $table->string('firstname');
            $table->string('lastname');
            $table->string('email')->unique();
            $table->string('password')->nullable();
            $table->unsignedBigInteger('user_type')->unsigned();
            $table->boolean('is_active')->default(1);
            $table->unsignedBigInteger('country')->nullable()->unsigned();
            $table->string('profile_picture')->nullable();
            $table->string('facebook_id')->nullable();
            $table->string('google_id')->nullable();
            $table->timestamps();
        });

        Schema::table('users', function (Blueprint $table) {            
            $table->foreign('user_type')->references('id')->on('user_type')->onDelete('restrict');
            $table->foreign('country')->references('id')->on('country')->onDelete('restrict');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}
0 likes
6 replies
s4muel's avatar

what is your user_type.id column type? it has to be 'unsigned big integer' ($table->bigIncrements('id'); is fine)

and does your user_type and country tables exist before running this migration (in other words, is the timestamp lower than for the users table)?

neba0317's avatar

it is just id

<?php

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

class UserType extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('user_type', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('user_type');
    }
}
s4muel's avatar

i suppose that is the case, the columns must match (length and signed/unsigned attribute), try changing that

Snapey's avatar

As you have been advised, the ORDER of migrations matters

Did you create user_types first?

neba0317's avatar

no users table is created first but it works in windows

s4muel's avatar

i advise you:

  • change the order of migrations, so everything that is referenced is migrated first (user_type and country in this case), you can do that by changing the timestamp in the migration filename (of course, depends whether your app is under development, or deployed to production. in that case, some manual tweaking of the migrations table data might be needed)
  • align the id and foreign key column "types" by using $table->bigIncrements('id') instead of $table->id(); in user_type migration

Please or to participate in this conversation.