Framework: Can't create a not nullable generated column in mysql

Created on 7 Feb 2020  路  7Comments  路  Source: laravel/framework

  • Laravel Version: 6.14.0
  • PHP Version: 7.4.0
  • Database Driver & Version: MySQL 8.0.19

Description:

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.

Steps To Reproduce:

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

All 7 comments

@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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

SachinAgarwal1337 picture SachinAgarwal1337  路  3Comments

shopblocks picture shopblocks  路  3Comments

kerbylav picture kerbylav  路  3Comments

felixsanz picture felixsanz  路  3Comments