Skip to content

Cannot add NOT NULL data constraint with Schema Builder using SQLite3 #2120

@rmariuzzo

Description

@rmariuzzo

Actually it is possible to define a NOT NULL column while creating a table in SQLite using Laravel's schema builder. But, inserting a null value doesn't issues a data constraint error, thus allowing the null value being inserted.

As seen in the docs for SQLite3, to define a working NOT NULL data constraint we need to append a conflict cause, otherwise it will not do anything.

I was looking through the source of Illuminate\Database\Schema\Blueprint and Illuminate\Database\Schema\Builder and cannot see a way of adding that on-conflict clause. I'm wondering if it is not possible to have a method that could allow appending raw string for a column. I believe it could defeat the purpose of being database agnostic, that's why I'm creating this issue here.

To reproduce the explained issue run the following code (using a SQLite database):

Schema::drop('test');
Schema::create('test', function($table)
{
    $table->increments('id')->unique();
    $table->string('password', 64);
});

DB::table('test')->insert([
    'password' => null,
]);

var_dump(DB::table('test')->get());

The desired SQL would look as follow for adding the aforementioned on-conflict clause:

CREATE TABLE TEST (
    ID INTEGER PRIMARY KEY, 
    PASSWORD VARCHAR NOT NULL ON CONFLICT FAIL);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions