I am trying to use UUID for my next project and have added a column id, which is a UUID type primary key.
I have another field in table that makes an internal reference ( foreign key ) to the id field. When I try to migrate the migration file it gives an obvious error because ( I guess ) mySQL does not allow you to create foreign key constraint on CHAR type column.
Is there any workaround or do I need to use another Auto-increment/ Integer type column.
Code for my migration file is:
$table->uuid('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->softDeletes();
$table->uuid('deleted_by_id')->nullable();
$table->foreign('deleted_by_id')->references('id')->on('users');
$table->rememberToken();
$table->timestamps();
$table->primary('id');
Error that I get:
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table `laravel_026db`.`#sql-5ec_16d` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `users` add constraint `users_deleted_by_id_foreign` foreign key (`deleted_by_id`) references `users` (`id`))