Laravel Unit Testing Migration fails

3.3k views Asked by At

I am trying to run PHPUnit test. I have setup SQLite in :memory for the testing environment. In my setup, I call Artisan::call('migrate') but then I get the following error:

General error: 1 Cannot add a NOT NULL column with default value NULL (SQL: alter table "admins" add column "title" text not null)

Basically, any migration file that is modifying an existing table returns an error. Why?

Here is the file migration is complaining about:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class AddTitleToAdminsTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('admins', function(Blueprint $table)
        {
            $table->text('title');
        });
    }


    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('admins', function(Blueprint $table)
        {
            $table->dropColumn('title');
        });
    }

}
1

There are 1 answers

0
lukasgeiter On BEST ANSWER

I did some research and found this post on Stack Overflow.

It appears that SQLite has a problem with columns that are NOT NULL but have no default value. When in the same situation, MySQL just uses an empty string (for varchar) or "0" (for numbers). So it kind of has built in default values.

To solve your problem you can either make the column nullable (only if you want it to be nullable) or define a default.

$table->text('title')->nullable();

$table->text('title')->default('');