11

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?

Aleksej_Shherbak
  • 2,757
  • 5
  • 34
  • 71

2 Answers2

28

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!

Sindhara
  • 1,423
  • 13
  • 20
3

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');
        });
    }
}
Alpy
  • 759
  • 6
  • 9
  • 1
    Why? I can use `NOW()` for example. And it's work. I have updated my code to `$table->dateTime('answer_token')->default(DB::raw('NOW()'));` and it works nice. I can see default dateTime values in my database. – Aleksej_Shherbak Feb 25 '19 at 13:17
  • NOW() it may be an exception but see here https://forums.mysql.com/read.php?10,91149,91161#msg-91161 – Alpy Feb 25 '19 at 13:25
  • Sorry but I don't understand. I have not written a trigger for the `NOW()` and it works. Which difference between `NOW()` and `UUID()`. Both are functions, right? From your link I'he understood that I need to make a trigger for a calling `UUID()`. So why I don't have to do it for `NOW()`? – Aleksej_Shherbak Feb 25 '19 at 13:36
  • Normally it should not work `With one exception, the default value specified in a DEFAULT clause must be a literal constant; it cannot be a function or an expression. This means, for example, that you cannot set the default for a date column to be the value of a function such as NOW() or CURRENT_DATE. ` https://dev.mysql.com/doc/refman/5.7/en/data-type-defaults.html as for your issue the trigger or pre-query should solve it. – Alpy Feb 25 '19 at 13:48