Skip to content

Conversation

ShawnMcCool
Copy link

DB Driver: SQLITE

// migration up
Schema::table('users', function($table)
{
$table->string('first_name');
$table->string('last_name');
});

$ php artisan migrate
SQLSTATE[HY000]: General error: 1 Cannot add a NOT NULL column with default value NULL

SQL: ALTER TABLE "users" ADD COLUMN "first_name" VARCHAR NOT NULL

Bindings: array ()

// controller code

$user = new User;

$user->fill(array(
'email' => 'bob@bob.com',
'username' => 'cat'
));

$user->save();

// web error

Unhandled Exception
Message:

SQLSTATE[23000]: Integrity constraint violation: 19 users.real_name may not be NULL

SQL: INSERT INTO "users" ("email", "username", "updated_at", "created_at") VALUES (?, ?, ?, ?)

Bindings: array (
0 => 'bob@bob.com',
1 => 'cat',
2 => '2012-03-07 19:37:34',
3 => '2012-03-07 19:37:34',
)

Location:

D:\web\new-street-works\laravel\database\connection.php on line 200

Stack Trace:
#0 D:\web\new-street-works\laravel\database\connection.php(141): Laravel\Database\Connection->execute('INSERT INTO "us...', Array)
#1 D:\web\new-street-works\laravel\database\query.php(736): Laravel\Database\Connection->query('INSERT INTO "us...', Array)
#2 D:\web\new-street-works\bundles\eloquent\model.php(437): Laravel\Database\Query->insert_get_id(Array, NULL)
#3 D:\web\new-street-works\bundles\auth\models\user.php(48): Eloquent\Model->save()
#4 D:\web\new-street-works\application\controllers\home.php(17): User->save()
#5 [internal function]: Home_Controller->action_index()
#6 D:\web\new-street-works\laravel\routing\controller.php(241): call_user_func_array(Array, Array)
#7 D:\web\new-street-works\laravel\routing\controller.php(201): Laravel\Routing\Controller->response('index', Array)
#8 D:\web\new-street-works\laravel\routing\controller.php(80): Laravel\Routing\Controller->execute('index', Array)
#9 D:\web\new-street-works\laravel\routing\route.php(139): Laravel\Routing\Controller::call('home@index', Array)
#10 D:\web\new-street-works\laravel\routing\route.php(110): Laravel\Routing\Route->response()
#11 D:\web\new-street-works\laravel\laravel.php(173): Laravel\Routing\Route->call()
#12 D:\web\new-street-works\public\index.php(52): require('D:\web\new-stre...')
#13 {main}

@taylorotwell
Copy link
Member

Shawn, would you mind either re-committing this with the -s flag set or agreeing to the developer certificate of origin in this comment chain?

@ShawnMcCool
Copy link
Author

I agree to the developer certificate of origin.

taylorotwell added a commit that referenced this pull request Mar 7, 2012
made all sqlite fields nullable to fix various problems with the difference in sqlite's null functionality
@taylorotwell taylorotwell merged commit 20f62b5 into laravel:master Mar 7, 2012
@franzliedke
Copy link
Contributor

Interesting. @ShawnMcCool: can you comment a little more on the "various problems"? I am working on the new database layer for FluxBB and would possibly make this kind of change, too - but I want to know why.

@ShawnMcCool
Copy link
Author

As you can see here:

SQLSTATE[23000]: Integrity constraint violation: 19 users.real_name may not be NULL

SQL: INSERT INTO "users" ("email", "username", "updated_at", "created_at") VALUES (?, ?, ?, ?)

Bindings: array (
0 => 'bob@bob.com',
1 => 'cat',
2 => '2012-03-07 19:37:34',
3 => '2012-03-07 19:37:34',
)

A field created without ->nullable() will error when a record is being created without that field being declared. In this case the real_name field was declared and left un-nullable. (somehow I left out this schema declaration from my original comment)

Schema::table('users', function($table) 
{ 
    $table->create(); 

    // Columns 
    $table->increments('id'); 
    $table->timestamps(); 
    $table->string('username'); 
    $table->string('real_name'); 
    $table->string('email'); 
    $table->string('password'); 
    $table->string('reset_password_hash'); 
});

@pondermatic
Copy link

When a field is defined with the NOT NULL constraint and a default value is not defined, inserting a record without declaring that field is a constraint violation and SHOULD result in an error.

The solution to the original problem isn't to have Laravel eliminate the NOT NULL constraint feature, but to instead define a default value or expression for fields that may not always be declared in an INSERT.

zoe-edwards pushed a commit to zoe-edwards/laravel that referenced this pull request Oct 14, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants