Need help with label references in my fixtures


#1

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


#2

On May 9, 6:40 am, Brian removed_email_address@domain.invalid wrote:

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 ?

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.

Fred


#3

On May 9, 11:39 am, Frederick C. removed_email_address@domain.invalid
wrote:

On May 9, 6:40 am, Brian removed_email_address@domain.invalid wrote:

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,

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:

sqlite3.exe db/test.sqlite3
sqlite> .schema caches
CREATE TABLE “caches” (“id” INTEGER PRIMARY KEY AUTOINCREMENT NOT
NULL, “capacit
y” integer, “structure_id” integer, “created_at” datetime,
“updated_at” datetime
);

So no surprises here. The structure_id column is there as I would
expect.


#4

On May 9, 1:18 pm, Frederick C. removed_email_address@domain.invalid
wrote:

Does removing the structure: line make a difference ?

Fred

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.


#5

On May 9, 5:54 pm, Brian removed_email_address@domain.invalid wrote:

On May 9, 11:39 am, Frederick C. removed_email_address@domain.invalid
sqlite> .schema caches
CREATE TABLE “caches” (“id” INTEGER PRIMARY KEY AUTOINCREMENT NOT
NULL, “capacit
y” integer, “structure_id” integer, “created_at” datetime,
“updated_at” datetime
);

So no surprises here. The structure_id column is there as I would
expect.

Does removing the structure: line make a difference ?

Fred


#6

On May 9, 6:33 pm, Brian removed_email_address@domain.invalid wrote:

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

in an initializer.

Fred


#7

Wait, I can answer my own question. To find the model it’s using
“singularize” and now I can see the error:

“caches”.singularize
=> “cach”

Thanks again for your help!


#8

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?

On May 9, 3:04 pm, Frederick C. removed_email_address@domain.invalid