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

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

Closed
capripio opened this issue Dec 3, 2016 · 5 comments
Closed

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

capripio opened this issue Dec 3, 2016 · 5 comments

Comments

@capripio
Copy link

capripio commented Dec 3, 2016

  • 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

@capripio
Copy link
Author

capripio commented Dec 3, 2016

I assume same error on stored function but not tested!

@themsaid
Copy link
Member

themsaid commented Dec 5, 2016

Can you please share more details about the issue? I don't seem to understand. Can you share the sql output of the migration that fails and the complete error message you get from the DB engine?

@capripio
Copy link
Author

capripio commented Dec 6, 2016

here you go:

[Illuminate\Database\QueryException]
  SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error i
  n your SQL syntax; check the manual that corresponds to your MariaDB server
   version for the right syntax to use near 'not null, `created_at` timestamp
   null, `updated_at` timestamp null) default char' at line 1 (SQL: create ta
  ble `orders` (`id` int unsigned not null auto_increment primary key, `user_
  id` int not null, `car_id` int not null, `reservation_id` int not null, `de
  tails` text null, `status` enum('RESERVED', 'CONFIRM', 'INPROGRESS', 'COMPL
  ETED', 'PENDING', 'CANCELLED') not null default 'RESERVED', `gross_amount`
  decimal(8, 2) not null default '0', `discount_percentage` double(5, 2) not
  null default '0', `discount_amount` decimal(8, 2) not null default '0', `ne
  t_amount` decimal(8, 2) as (gross_amount - discount_amount) not null, `crea
  ted_at` timestamp null, `updated_at` timestamp null) default character set
  utf8 collate utf8_unicode_ci)



  [PDOException]
  SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error i
  n your SQL syntax; check the manual that corresponds to your MariaDB server
   version for the right syntax to use near 'not null, `created_at` timestamp
   null, `updated_at` timestamp null) default char' at line 1

@fernandobandeira
Copy link
Contributor

I've opened a PR to fix this, I was waiting because I thought you would submit one...
And yes, this seems to occur with the storedAs too, I've slightly modified your fix and modified the tests...

@capripio
Copy link
Author

capripio commented Dec 6, 2016

@fernandobandeira thanks I really don't know much about sending patch stuff, but I am glad you did!

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

No branches or pull requests

3 participants