0

I have a model called Issue that has the following database structure :

class CreateIssues < ActiveRecord::Migration
  def change
    create_table :issues do |t|
      t.string :path, null: false
      t.string :filename, null: false
      t.string :name, null: false
      t.string :year
      t.integer :number
      t.timestamps null: false
    end
  end
end

In my model tests, I use the following line :

issue = Issue.create path: "test_path", filename: "test_filename", name: "test_name"

... which raises an exception when saving :

 SQLite3::ConstraintException: NOT NULL constraint failed:
 issues.path: INSERT INTO "issues" ("created_at", "updated_at") VALUES (?, ?)

From what I understand, when calling create or new/save, rails starts by inserting an entry with only the timestamps. I would expect the insert to contain all of the values that I have passed to the create method. Am I missing a step in the creation process?

EDIT :

Basic information and steps:

  • Using SQLite3
  • Using Rails 4.2.1
  • Using RSpec
  • Generated the Issue model using be rails generate model Issue
  • Added the NULL constraints afterwords by hand
  • Did be rake db:migrate successfully
  • Tried my code in various model spec files

Tried other models, I get generated SQL that only includes the timestamps.

EDIT 2: Here is my Issue model :

class Issue < ActiveRecord::Base
  has_one :pending_issue
  has_one :watched_issue
  has_many :unmatched_issues

  validates :path, presence: true
  validates :filename, presence: true
  validates :name, presence: true

  attr_accessor :path
  attr_accessor :filename
  attr_accessor :name
  attr_accessor :year
  attr_accessor :number
end
Corb3nik
  • 1,177
  • 7
  • 26

2 Answers2

1

Your result is anomalous. This is what I get:

# migration
class CreateIssues < ActiveRecord::Migration
  def change
    create_table :issues do |t|
      t.string :path, null: false

      t.timestamps null: false
    end
  end
end

Then in the console:

Issue.create path: "path"
# (0.1ms)  begin transaction
# SQL (0.3ms)  INSERT INTO "issues" ("path", "created_at", "updated_at") VALUES (?, ?, ?)  [["path", "path"], ["created_at", "2015-06-21 16:55:08.304653"], ["updated_at", "2015-06-21 16:55:08.304653"]]
# (0.8ms)  commit transaction
# => #<Issue id: 1, path: "path", created_at: "2015-06-21 16:55:08", updated_at: "2015-06-21 16:55:08"> 

My test:

require "test_helper"

describe Issue do

  it "can be created" do
    Issue.create path: "test_path"
  end
end

passes. No exception.

What I think may have happened is that your database schema has become out of date on your test database.

Firstly, does a similar 'create' work in the development (default) rails console? If so, it's a big indication that something is very different in your test and development environments.

What happens if you force the console to use the test environment?

RAILS_ENV=test rails c

Does it work here?

I think you may need to manually rollback your test database to the migration which first created your issues table, then re-migrate

RAILS_ENV=test rake db:rollback

Keep applying until you reach the appropriate migration, then:

RAILS_ENV=test rake db:migrate

Does that help?

EDIT:

This appears to be the issue:

attr_accessor :path
attr_accessor :filename
attr_accessor :name
attr_accessor :year
attr_accessor :number

These will overwrite the Rails default accessors, at a guess. They are certainly not needed.

A Fader Darkly
  • 3,516
  • 1
  • 22
  • 28
  • Can I do `rake db:reset` and start from scratch? – Corb3nik Jun 21 '15 at 17:23
  • Did as you said, doesn't work. Running the console in test environnment gives the same result and rollback doesn't solve the issue either – Corb3nik Jun 21 '15 at 17:28
  • Answer updated. Remove your attr_accessor declarations: Rails does not need them, and they may well be overwriting the ActiveRecord functionality. I will run some tests. – A Fader Darkly Jun 21 '15 at 17:40
  • YES! I think it worked!! Here is a link to a SO question : http://stackoverflow.com/questions/17371334/how-is-attr-accessible-used-in-rails-4 – Corb3nik Jun 21 '15 at 17:43
  • 2
    Yes, if I add an attr_accessor, I get your error: ```ActiveRecord::StatementInvalid: SQLite3::ConstraintException: NOT NULL constraint failed: issues.path: INSERT INTO "issues" ("created_at", "updated_at") VALUES (?, ?)``` – A Fader Darkly Jun 21 '15 at 17:44
0

Did you accidentally omit attr_accessible for :path?

David Aldridge
  • 51,479
  • 8
  • 68
  • 96