Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using UUID as default value in a laravel migration

I have the following migration:

<?php

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

class UpdateRatingGameUserAnswersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('rating_games_user_answers', function (Blueprint $table) {
            $table->uuid('answer_token')->default(DB::raw('UUID()'));
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('rating_games_user_answers', function (Blueprint $table) {
            $table->dropColumn('answer_token');
        });
    }
}

As you can see I'm trying to set UUID as default value. I've seen it here

But when I run php artisan migrate I see the following:

enter image description here

What is wrong?

like image 980
Aleksej_Shherbak Avatar asked Feb 25 '19 12:02

Aleksej_Shherbak


People also ask

What is the use of UUID in laravel?

UUIDs are created by your application, so you don't have to wait for the database server to create new users. Since you create your UUIDs independent of the database, you can split your data between multiple database servers. UUIDs are secure, as they're randomly generated and unique, so they can't be guessed.

Is laravel UUID unique?

Laravel uuid is known as the universally unique identifier, which was 128 bits value and used to identify the records of the table.

How do I change my UUID ID in laravel?

With your lovely IDE, open the project and go to the 'database/migration' path. If you haven't run migration yet, just modify the user migration file. Or, create another migration file. Change the 'id' type to UUID.


2 Answers

As I came across this question when having the same issue: With MySQL 8 you can use functions as default values. But you need to put paranthesis around them. So this works:

$table->uuid('uid')->default(DB::raw('(UUID())'));

But this won't be compatible with MySQL 5.7!

like image 85
Sindhara Avatar answered Oct 09 '22 14:10

Sindhara


You cannot use mySQL function as default value it should be either set or you use a trigger.

try please like this:

<?php

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

class UpdateRatingGameUserAnswersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('rating_games_user_answers', function (Blueprint $table) {
            $uuid = DB::raw('select UUID()');
            $table->uuid('answer_token')->default($uuid);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('rating_games_user_answers', function (Blueprint $table) {
            $table->dropColumn('answer_token');
        });
    }
}
like image 35
Alpy Avatar answered Oct 09 '22 15:10

Alpy