0

I'm working with Laravel and when I run php artisan migrare I got this error:

Illuminate\Database\QueryException : SQLSTATE[42804]: Datatype mismatch: 7 ERROR: foreign key constraint "clients_record_id_foreign" cannot be implemented DETAIL: Key columns "record_id" and "id" are of incompatible types: uuid and bigint. (SQL: alter table "clients" add constraint "clients_record_id_foreign" foreign key ("record_id") references "records" ("id") on delete cascade)}

I'm not sure how to solve this. Somebody could help me?

These are my migration files.

create_records_table.php

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateRecordsTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('records', function (Blueprint $table) {
        //$table->bigIncrements('id');

        $table->uuid('id')->primary();

        $table->uuid('device_site_id');
        $table->foreign('device_site_id')->references('id')->on('device_site')->onDelete('cascade');

        $table->string('gw_address',16);
        $table->string('gw_port',6);
        $table->string('gw_id');
        $table->string('ip',16);
        $table->string('mac',18);
        $table->string('url');
        $table->string('user_agent');

        $table->timestamps();
    });
    DB::statement('ALTER TABLE records ALTER COLUMN id SET DEFAULT uuid_generate_v4();');
}

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

create_clients_table.php

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateClientsTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('clients', function (Blueprint $table) {
        $table->uuid('id')->primary();

        $table->uuid('record_id');
        $table->foreign('record_id')->references('id')->on('records')->onDelete('cascade');

        $table->string('name');
        $table->string('telephone');
        $table->string('email');
        $table->string('gender');

        $table->timestamps();
    });
    DB::statement('ALTER TABLE clients ALTER COLUMN id SET DEFAULT uuid_generate_v4();');
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('clients');
}
}
3
  • You have //$table->bigIncrements('id'); in your records table. Did you comment it out and add the uuid after you migrated the table? If so, you'll have to refresh your migrations, which will drop all of the tables and recreate it. If you don't want to do that, you'll have to modify it manually.
    – aynber
    Apr 15, 2020 at 13:08
  • Looks like you commented out $table->bigIncrements('id'); in your records table migration, maybe you modified that column after you have already migrated. Then you tried to migrate your clients table. Try php artisan migrate:fresh command too rollback all migrations and re migrate them. Apr 15, 2020 at 13:08
  • Thank you! This worked perfect -> php artisan migrate:fresh Apr 15, 2020 at 13:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.