Skip to content

Having issue with not null keywork while using ->virtualAs #16643

@capripio

Description

@capripio
  • Laravel Version: 5.3.26
  • PHP Version: 5.6
  • Database Driver & Version: MariaDB (10.1.13-MariaDB)

Description:

Got syntax error using MariaDB version 10.1.13
('not null, created_at timestamp)
I think when adding virtual column not null will be not as virtual generated column can't be nulled.
I used --prepend method and removed not null keyword and query get accepted.

Steps To Reproduce:

make migration using following query:

Schema::create('orders', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->text('details')->nullable();
            $table->enum('status',['NEW','COMPLETED','PENDING','INPROGRESS','CANCELLED'])->default('NEW');
            $table->decimal('gross_amount',5,2)->default(0);
            $table->float('discount_percentage',3,2)->default(0);
            $table->decimal('discount_amount',5,2)->default(0);
            $table->decimal('net_amount',5,2)->virtualAs('gross_amount - discount_amount'); //error here
            $table->timestamps();
        });

My Workaround (author may have better way):

 /**
     * Get the SQL for a nullable column modifier.
     *
     * @param  \Illuminate\Database\Schema\Blueprint  $blueprint
     * @param  \Illuminate\Support\Fluent  $column
     * @return string|null
     */
    protected function modifyNullable(Blueprint $blueprint, Fluent $column)
    {
        if (! is_null($column->virtualAs))
            return;
        return $column->nullable ? ' null' : ' not null';
    }

@ /vendor/laravel/framework/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions