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');
}
}
//$table->bigIncrements('id');
in your records table. Did you comment it out and add theuuid
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.$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. Tryphp artisan migrate:fresh
command too rollback all migrations and re migrate them.