Home > Enterprise >  Laravel bug: making column nullable stops it from being added to table
Laravel bug: making column nullable stops it from being added to table

Time:12-14

I have a SQLite database with the following migration defined:

    Schema::create("views", function(Blueprint $table){

        $table->integer("match_id");
        $table->string("pov");
        $table->string("teams");     
        $table->text("moves");

        $table->bigIncrements("id");    
        $table->date("created_at");
        $table->date("updated_at"); 
    });

Note: id is the primary key of the table, match_id should just be treated as any other column.

This all works just fine, but I want match_id to be nullable. I tried changing the $table -> integer("match_id") to the following:

$table->integer("match_id")->nullable()->change();
//Also tried $table->integer("match_id")->nullable(true)->change();

I also ran 'composer require doctrine/dbal', which I saw online

When I try running php artisan migrate, it says there is nothing to migrate. And when I run php artisan migrate:fresh, for some reason the match_id column just doesn't get added to the database at all, even though every other column works just fine. Any reason this might be?

CodePudding user response:

By using this

$table->foreignId('match_id')->nullable();
$table->integer('match_id')->nullable();

In Laravel 7

you can make "match_id" (nullable & integer) or foreign-key by using this code

$table->foreignId('test_id')->nullable();
$table->integer('tint_id')->nullable();

As i used it in picture here is code enter image description here

and here is my db enter image description here

CodePudding user response:

Since migrate:fresh is an option (as pointed by @xenooooo), just edit your original migration to:

Schema::create("views", function(Blueprint $table){


        $table->bigIncrements("id");
        $table->integer("match_id")->nullable();
        $table->string("pov");
        $table->string("teams");     
        $table->text("moves");
        $table->timestamps();

    });

This should fix the problem. With migrate:fresh you don't need an second migration just to change, since it will drop all tables and create everything again. Also I've changed the order of the primary key (just in case of this be the problem of migrate:fresh not creating the match_id column), and changed the timestamps format. In Laravel you don't need to create manually the timestamps, "$table->timestamps()" do it for you :)

  • Related