# Need help with label references in my fixtures

**URL:** <https://discuss.rubyonrails.org/t/need-help-with-label-references-in-my-fixtures/42342>\
**Category:** rubyonrails-talk\
**Created:** [May 9, 2009, 5:40am UTC](https://discuss.rubyonrails.org/t/need-help-with-label-references-in-my-fixtures/42342 "2009-05-09T05:40:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian10](https://avatars.discourse-cdn.com/v4/letter/b/bc8723/32.png) [@Brian10](https://discuss.rubyonrails.org/u/Brian10)\
**Post date:** [May 9, 2009, 5:40am UTC](https://discuss.rubyonrails.org/t/need-help-with-label-references-in-my-fixtures/42342/1 "2009-05-09T05:40:05Z")

</div>

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 &nbsp;&nbsp;belongs\_to :structure end

Schema (schema.rb): create\_table "caches", :force =\> true do |t| &nbsp;&nbsp;&nbsp;&nbsp;t.integer "capacity" &nbsp;&nbsp;&nbsp;&nbsp;t.integer "structure\_id" &nbsp;&nbsp;&nbsp;&nbsp;t.datetime "created\_at" &nbsp;&nbsp;&nbsp;&nbsp;t.datetime "updated\_at" &nbsp;&nbsp;end

Fixture (caches.yml): one: &nbsp;&nbsp;capacity: 1 &nbsp;&nbsp;structure:

two: &nbsp;&nbsp;capacity: 1 &nbsp;&nbsp;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 &nbsp;&nbsp;belongs\_to :location end

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [May 9, 2009, 3:39pm UTC](https://discuss.rubyonrails.org/t/need-help-with-label-references-in-my-fixtures/42342/2 "2009-05-09T15:39:51Z")

</div>

> 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.

Fred

---

<div class="post-metadata">

**Author:** ![Brian10](https://avatars.discourse-cdn.com/v4/letter/b/bc8723/32.png) [@Brian10](https://discuss.rubyonrails.org/u/Brian10)\
**Post date:** [May 9, 2009, 4:54pm UTC](https://discuss.rubyonrails.org/t/need-help-with-label-references-in-my-fixtures/42342/3 "2009-05-09T16:54:15Z")

</div>

> \> 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.

&nbsp;&nbsp;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:

> sqlite3.exe db/test.sqlite3 .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.

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [May 9, 2009, 5:18pm UTC](https://discuss.rubyonrails.org/t/need-help-with-label-references-in-my-fixtures/42342/4 "2009-05-09T17:18:41Z")

</div>

Does removing the structure: line make a difference ?

Fred

---

<div class="post-metadata">

**Author:** ![Brian10](https://avatars.discourse-cdn.com/v4/letter/b/bc8723/32.png) [@Brian10](https://discuss.rubyonrails.org/u/Brian10)\
**Post date:** [May 9, 2009, 5:33pm UTC](https://discuss.rubyonrails.org/t/need-help-with-label-references-in-my-fixtures/42342/5 "2009-05-09T17:33:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [May 9, 2009, 7:04pm UTC](https://discuss.rubyonrails.org/t/need-help-with-label-references-in-my-fixtures/42342/6 "2009-05-09T19:04:10Z")

</div>

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| &nbsp;&nbsp;&nbsp;inflect.irregular 'cache', 'caches' end

in an initializer.

Fred

---

<div class="post-metadata">

**Author:** ![Brian10](https://avatars.discourse-cdn.com/v4/letter/b/bc8723/32.png) [@Brian10](https://discuss.rubyonrails.org/u/Brian10)\
**Post date:** [May 10, 2009, 12:14am UTC](https://discuss.rubyonrails.org/t/need-help-with-label-references-in-my-fixtures/42342/7 "2009-05-10T00:14:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Brian10](https://avatars.discourse-cdn.com/v4/letter/b/bc8723/32.png) [@Brian10](https://discuss.rubyonrails.org/u/Brian10)\
**Post date:** [May 10, 2009, 12:17am UTC](https://discuss.rubyonrails.org/t/need-help-with-label-references-in-my-fixtures/42342/8 "2009-05-10T00:17:32Z")

</div>

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!
