Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Rails 3: :null => false, tests and ActiveRecord::StatementInvalid: SQLite3::SQLException XXXXX may not be NULL

#4007

Okay, long description here, but follow me...


Create a new Rails project

rails testapp
cd testapp

Create your first resource

rails generate resource User

Create a migration that sets :null => false on a field

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.string :email, :null => false
      t.timestamps
    end
  end

  def self.down
    drop_table :users
  end
end

Run your migration

rake db:migrate

Try to run your default "test truth" tests

rake test:units

This gives the following error message:

Loaded suite /Users/marc/.bundle/ruby/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
E
Finished in 0.066302 seconds.

  1) Error:
test_the_truth(UserTest):
ActiveRecord::StatementInvalid: SQLite3::SQLException: users.email may not be NULL: INSERT INTO "users" ("created_at", "updated_at", "id") VALUES ('2010-02-20 10:39:59', '2010-02-20 10:39:59', 298486374)
    

1 tests, 0 assertions, 0 failures, 1 errors
rake aborted!
Command failed with status (1): [/usr/local/bin/ruby -I"lib:test" "/Users/m...]

The same error pops up for any actual tests you create. The hangup seems to be any columns set to :null => false

Reported by Marc Love · February 20th, 2010 @ 10:43 AM

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. José Valim
    José Valim
    • State changed from new to invalid

    Yes, this is expected. You have to configure your fixtures or given the proper args to User.create.

    February 23rd, 2010 @ 09:30 PM