Framework: DEFAULT value in table migration cannot be executed if it is a function

Created on 22 Nov 2015  ·  3Comments  ·  Source: laravel/framework

When i specify a function name as a table column default, this is wrapped into quotes so when executed the function isn't run, its saved as a string.

This gives an error fortunately because UUID col can't have a default string value of this type.

Schema::create('products', function(Blueprint $table)
        {
            $table->uuid('id')->default('gen_random_uuid()')->primary('products_pkey');
            $table->string('name')->nullable();
            $table->string('max_projects')->nullable();
            $table->timestamps();
            $table->softDeletes();
        });

Most helpful comment

Try with DB::raw:

 $table->uuid('id')->default(DB::raw('gen_random_uuid()'))->primary('products_pkey');

All 3 comments

Try with DB::raw:

 $table->uuid('id')->default(DB::raw('gen_random_uuid()'))->primary('products_pkey');

You can't use a function as a default value in MySQL. It has to be a constant. To generate a UUID, you'd have to either use a trigger or do it manually.

gen_random_uuid() is a Postgre function.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kerbylav picture kerbylav  ·  3Comments

RomainSauvaire picture RomainSauvaire  ·  3Comments

lzp819739483 picture lzp819739483  ·  3Comments

progmars picture progmars  ·  3Comments

JamborJan picture JamborJan  ·  3Comments