Xethron / migrations-generator

Laravel Migrations Generator: Automatically generate your migrations from an existing database schema.
MIT License
3.32k stars 588 forks source link

DB::raw breaks postgres default booleans #110

Closed shemgp closed 7 years ago

shemgp commented 7 years ago

When there's a DB::raw('0') in a boolean's default value, in PostgreSQL, the following error occurs:

  [PDOException]                                                                                                                 
  SQLSTATE[42804]: Datatype mismatch: 7 ERROR:  column "separator" is of type boolean but default expression is of type integer  
  HINT:  You will need to rewrite or cast the expression.                                                                        

Here's a sample schema that generates the error:

                Schema::create('menus', function(Blueprint $table)
                {
                        $table->integer('id', true);
                        $table->string('name');
                        $table->string('label');
                        $table->integer('position')->default(0);
                        $table->string('icon')->nullable();
                        $table->boolean('separator')->default(DB::raw('0'));
                        $table->string('url')->nullable();
                        $table->boolean('enabled')->default(DB::raw('0'));
                        $table->integer('parent_id')->default(0);
                        $table->integer('route_id')->nullable();
                        $table->integer('permission_id')->nullable();
                        $table->timestamps();
                });

Here's the schema that doesn't have the error:

                Schema::create('menus', function(Blueprint $table)
                {
                        $table->integer('id', true);
                        $table->string('name');
                        $table->string('label');
                        $table->integer('position')->default(0);
                        $table->string('icon')->nullable();
                        $table->boolean('separator')->default(0);
                        $table->string('url')->nullable();
                        $table->boolean('enabled')->default(0);
                        $table->integer('parent_id')->default(0);
                        $table->integer('route_id')->nullable();
                        $table->integer('permission_id')->nullable();
                        $table->timestamps();
                });
shemgp commented 7 years ago

Just tested this code:

$default = 0;
if (in_array($default, ['CURRENT_TIMESTAMP']))
{
    echo 'found';
}

and it returns found. That's why the DB::raw's were being added.

Updated pull request to use strict mode for in_array.

Xethron commented 7 years ago

Love that you changed it to strict instead, much better solution!