I am new to Rails and running into problems loading fixtures. After
generating a bunch of scaffolding, I did a "rake db:migrate", followed
by "rake db:test:load", and then "rake db:fixtures:load". My goal is
to run the unit tests so that I can verify that everything is at a
good starting point before I do any real development. I get the
following error:
SQLite3::SQLException: table caches has no column named structure:
INSERT INTO "
caches" ("structure", "capacity") VALUES (NULL, 1)
Can somebody provide a good starting point for troubleshooting this
type of problem?
After a bit of searching, I think these are the three relevant code
snippets that I should be looking at.
Model (cache.rb):
class Cache < ActiveRecord::Base
belongs_to :structure
end
Schema (schema.rb):
create_table "caches", :force => true do |t|
t.integer "capacity"
t.integer "structure_id"
t.datetime "created_at"
t.datetime "updated_at"
end
Fixture (caches.yml):
one:
capacity: 1
structure:
two:
capacity: 1
structure:
All of this was generated for me, and it all matches up with what I
expect from documentation. I know if "belongs_to :structure" was
missing from my model I should expect the error I'm seeing now, so I'm
very suspicious of that being wrong in a subtle way.
Here's the model for structure if it matters (as far as I can tell, it
doesn't):
class Structure < ActiveRecord::Base
belongs_to :location
end
I am new to Rails and running into problems loading fixtures. After
generating a bunch of scaffolding, I did a "rake db:migrate", followed
by "rake db:test:load", and then "rake db:fixtures:load". My goal is
to run the unit tests so that I can verify that everything is at a
good starting point before I do any real development. I get the
following error:
you shouldn't need to do rake db:test:load or rake db:fixtures:load
SQLite3::SQLException: table caches has no column named structure:
INSERT INTO "
caches" ("structure", "capacity") VALUES (NULL, 1)
when does this happen ? when running one of the previously mentionned
commands, when running the units tests, at some other time ?
Can somebody provide a good starting point for troubleshooting this
type of problem?
After a bit of searching, I think these are the three relevant code
snippets that I should be looking at.
Model (cache.rb):
class Cache < ActiveRecord::Base
belongs_to :structure
end
Schema (schema.rb):
create_table "caches", :force => true do |t|
t.integer "capacity"
t.integer "structure_id"
t.datetime "created_at"
t.datetime "updated_at"
end
Fixture (caches.yml):
one:
capacity: 1
structure:
two:
capacity: 1
structure:
All of this was generated for me, and it all matches up with what I
expect from documentation. I know if "belongs_to :structure" was
missing from my model I should expect the error I'm seeing now, so I'm
very suspicious of that being wrong in a subtle way.
well first off, definitely worth checking if your test database has
all the columns you expect.
> SQLite3::SQLException: table caches has no column named structure:
> INSERT INTO "
> caches" ("structure", "capacity") VALUES (NULL, 1)
when does this happen ? when running one of the previously mentionned
commands, when running the units tests, at some other time ?
I get this when trying to load fixtures, or when trying to run a unit
test:
ruby unit/cache_test.rb
Loaded suite unit/cache_test
Started
E
Finished in 0.305 seconds.
1) Error:
test_the_truth(CacheTest):
ActiveRecord::StatementInvalid: SQLite3::SQLException: table caches
has no colum
n named structure: INSERT INTO "caches" ("structure", "capacity")
VALUES (NULL,
1)
well first off, definitely worth checking if your test database has
all the columns you expect.
That's a good idea. I figured out where to pick up the SQLite console
today so that I could do just that:
It does, but then I get similar errors loading other fixtures. Right
now, my cache fixture only has the two entries generated when I
created the model, but I'd like to add more meaningful test data in
there to start writing my unit tests. References to structures will
be fairly important.
ah, I know what's happening. The default pluralization rules rails has
screw up cache/caches, so when rails loads the fixtures it tries to
look for the wrong model class so the fixture data is inserted without
the structure having being turned into structure_id
Adding an inflector rule should do the trick eg stick
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'cache', 'caches'
end
Fred, thank you very much. This was exactly the problem. But I admit
that I'm still confused. Everything was created with the "generate"
script which presumably uses the same rules. And when I go into the
ruby console:
Loading development environment (Rails 2.2.2)
"cache".pluralize
=> "caches"
So it seems to get this correct even without the initializer. How did
it get it wrong when looking for the model class?