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');
}
}
<?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');
}
}
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