f7n
f7n

Reputation: 1674

Adding a NOT NULL Column to an Existing Table in Laravel SQLite

I'm trying to add a foreign key to an existing table:

public function up()
{
    Schema::table('employee', function (Blueprint $table) {
        $table->uuid("agency_id");
    }
}

and SQLite is throwing error:

General error: 1 Cannot add a NOT NULL column with default value NULL (SQL: alter table "employees" add column "agency_id" varchar not null)

I cannot add a default value of an empty string as the type is UUID. What can I do?

Upvotes: 1

Views: 256

Answers (1)

STA
STA

Reputation: 34678

SQLite would typically choose NULL, but you've specified it can't be NULL, so what should it be? It has no way of knowing.

When adding a table from scratch, you can specify NOT NULL. However, you can't do this when adding a column. SQLite's specification says you have to have a default for this. You could specify the default value, such as :

public function up()
{
    Schema::table('employee', function (Blueprint $table) {
        $table->uuid("agency_id")->nullable();
    }
}

Upvotes: 0

Related Questions