Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't create a not nullable generated column in mysql #31398

Closed
DrCake opened this issue Feb 7, 2020 · 7 comments
Closed

Can't create a not nullable generated column in mysql #31398

DrCake opened this issue Feb 7, 2020 · 7 comments

Comments

@DrCake
Copy link
Contributor

DrCake commented Feb 7, 2020

  • 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

@BenQoder
Copy link

BenQoder commented Feb 8, 2020

@DrCake adding nullable() to any migration column makes its nullable.

Remove nullable(false) from your location row.

@staudenmeir
Copy link
Contributor

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 (laravel/ideas#870).

@DrCake
Copy link
Contributor Author

DrCake commented Feb 9, 2020

@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'.

@DrCake
Copy link
Contributor Author

DrCake commented Feb 9, 2020

Is it possible to keep it nullable by default unless ->nullable(false) is called?

@staudenmeir
Copy link
Contributor

It would be possible, but it's definitely not an elegant solution.

@driesvints
Copy link
Member

As this is rather a change in behavior than a bug fix feel free to discuss this further on the ideas repo.

@mpyw
Copy link
Contributor

mpyw commented Feb 13, 2020

@staudenmeir @DrCake Thanks for advising and submitting PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants