When creating a generated column in my migration, the nullable() modifier method has no effect on the column that is created. It looks like this was disabled to fix an issue back in 5.3 but hasn't been re-enabled now that MySQL supports it.
The Migration
class CreateAddressesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('addresses', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->decimal('latitude');
$table->decimal('longitude');
$table->point('location', 4326)->nullable(false)->storedAs('Point(`latitude`, `longitude`)');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('addresses');
}
}
Running this migration, if you inspect the database you will see that the location column is nullable. So if you try and add a spatial index on the location column, you will get an error that you can't create the index on a column that is allowed to be null
@DrCake adding nullable() to any migration column makes its nullable.
Remove nullable(false) from your location row.
The fix was for MariaDB that (still) doesn't support null/not null on virtual/stored columns.
It's not trivial to have different implementations for MySQL and MariaDB without database/version-specific grammars (https://github.com/laravel/ideas/issues/870).
@BenQoder Adding false as the parameter to nullabe() is a manual way of specifying not null. I added it just to check that the default for staredAs isn't 'null' instaed of the normal 'not null'.
Is it possible to keep it nullable by default unless ->nullable(false) is called?
It would be possible, but it's definitely not an elegant solution.
As this is rather a change in behavior than a bug fix feel free to discuss this further on the ideas repo.
@staudenmeir @DrCake Thanks for advising and submitting PR!