1

I have a UUID column type which is a foreign key to another table and I want to make it nullable.

$table->uuid('event_id')->nullable()->change();

The Laravel documentation includes a list of column types which can be 'changed', and this does not include uuid, so no real surprise there, https://laravel.com/docs/5.6/migrations#modifying-columns

The database is MySQL, and when it's created the event_id field is a char, so I tried the following:

$table->char('event_id',36)->nullable()->change();

Got the same result as above (and again, no real surprise).

My question is: can I make this modification using Laravel'd DB Schema, or do I need to resort to raw SQL?

DatsunBing
  • 8,684
  • 17
  • 87
  • 172

1 Answers1

3

Like enum columns, you likely have to use raw SQL if you wish to change uuid on an existing database.

I usually would write something like this in the new migration to handle existing databases:

if (config('app.env')) === 'production') {
    DB::statement('ALTER ...');
}

Then I'd go back to the original migration and change the uuid to a char. This way, you don't have to worry about the raw SQL not working on your testing or local environments (in case of running sqlite).

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95