# Changing tablename without changing modelname

**URL:** <https://discuss.rubyonrails.org/t/changing-tablename-without-changing-modelname/56158>\
**Category:** rubyonrails-talk\
**Created:** [December 9, 2010, 9:09am UTC](https://discuss.rubyonrails.org/t/changing-tablename-without-changing-modelname/56158 "2010-12-09T09:09:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [December 9, 2010, 9:09am UTC](https://discuss.rubyonrails.org/t/changing-tablename-without-changing-modelname/56158/1 "2010-12-09T09:09:28Z")

</div>

Hello, I want to change some tablenames on my DB, but I dont want to change the modelnames in my RailsApp. So I write down this line of code in the model with the changed table: " set\_table\_name "newName" " . Also I changed in the migrationfiles the tablenames and renamed the fixtures.

But if I now run " rake db:fixtures:load " I get an MySql Error:

Mysql::Error: Unknown column 'foo' in 'field list': INSERT INTO `newTablename` (`price`, `name`, `size`, `type`, `description`, `provider`) VALUES (5.95, 'Pinot Grigio', '0.75l', 'wein', NULL, 'joeys')

Can anybody tell me what I do wrong?

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [December 9, 2010, 9:40am UTC](https://discuss.rubyonrails.org/t/changing-tablename-without-changing-modelname/56158/2 "2010-12-09T09:40:48Z")

</div>

Is that literally exactly what you get? column 'foo'? and 'newTableName'? If not can you show exactly what it says?

I also have country, rating, and buy\_again in my wine list, though I doubt if that is part your problem. 🙂

Colin

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [December 9, 2010, 10:50am UTC](https://discuss.rubyonrails.org/t/changing-tablename-without-changing-modelname/56158/3 "2010-12-09T10:50:33Z")

</div>

My old tablename was products and sizes. Now it's fos\_products and fos\_sizes. I write "set\_table\_name fos\_products" and "set\_table\_name fos\_sizes"in my products and sizes model.

After that I renamed my fixture files from products.yml and sizes.yml to fos\_products.yml and fos\_sizes.yml and I renamed in my migration files the "create table products" statement to "create table fos\_products" etc.

Now I dropped the DB-\> after that rake:db:create-\>rake db:migrate-\>rake:db:fixtures:load

Mysql::Error: Unknown column 'size' in 'field list': INSERT INTO `fos_products` (`price`, `name`, `size`, `type`, `description`, `provider`) VALUES (5.95, 'Pinot Grigio', '0.75l', 'wein', NULL, 'joeys')

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [December 9, 2010, 11:26am UTC](https://discuss.rubyonrails.org/t/changing-tablename-without-changing-modelname/56158/4 "2010-12-09T11:26:22Z")

</div>

Can you post db/schema.rb please so we can see what fields are in the table. Just the bit of the schema for fos\_products table.

Colin

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [December 9, 2010, 11:35am UTC](https://discuss.rubyonrails.org/t/changing-tablename-without-changing-modelname/56158/5 "2010-12-09T11:35:29Z")

</div>

#schema.rb

ActiveRecord::Schema.define(:version =\> 20101124114645) do &nbsp;&nbsp;... &nbsp;&nbsp;create\_table "fos\_products", :force =\> true do |t| &nbsp;&nbsp;&nbsp;&nbsp;t.string "name" &nbsp;&nbsp;&nbsp;&nbsp;t.decimal "price", :precision =\> 8, :scale =\> 2 &nbsp;&nbsp;&nbsp;&nbsp;t.string "description" &nbsp;&nbsp;&nbsp;&nbsp;t.integer "type\_id" &nbsp;&nbsp;&nbsp;&nbsp;t.integer "size\_id" &nbsp;&nbsp;&nbsp;&nbsp;t.integer "provider\_id" &nbsp;&nbsp;&nbsp;&nbsp;t.date "valid\_date" &nbsp;&nbsp;&nbsp;&nbsp;t.integer "card\_nr" &nbsp;&nbsp;&nbsp;&nbsp;t.integer "card\_char" &nbsp;&nbsp;&nbsp;&nbsp;t.datetime "created\_at" &nbsp;&nbsp;&nbsp;&nbsp;t.datetime "updated\_at" &nbsp;&nbsp;&nbsp;&nbsp;t.boolean "inactive", :default =\> false &nbsp;&nbsp;&nbsp;&nbsp;t.integer "editor" &nbsp;&nbsp;end &nbsp;&nbsp;... end

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [December 9, 2010, 11:52am UTC](https://discuss.rubyonrails.org/t/changing-tablename-without-changing-modelname/56158/6 "2010-12-09T11:52:24Z")

</div>

So what do you deduce from that? Your fixtures load is trying to write to a size field, yet there is no such field, only a size\_id. Do I deduce that you have a sizes class with Product belongs\_to size, and in the fixtures you are trying to set a particular size value into the fos\_products table using the association?

If so then this does not seem to work with non-standard table names. Whether there is a way round this I do not know. The best solution is to chuck the fixtures in the bin and move over to Factories, probably Machinist or Factory Girl. I prefer the former. Once you work out how to use them you will not regret the move. If you just want to get it going immediately then I suggest reverting to the manual method for setting size\_id, by providing a number for it and for the entries in the sizes table. I guess there are only a couple of sizes so this should not be a problem. A few global search/replace operations in the fixtures should do it. Of course this paragraph started with an 'if' so it may not be this at all.

Colin

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [December 9, 2010, 12:08pm UTC](https://discuss.rubyonrails.org/t/changing-tablename-without-changing-modelname/56158/7 "2010-12-09T12:08:00Z")

</div>

I do this: "I suggest reverting to the manual method for setting size\_id, by providing a number for it and for the entries in the sizes table. "

Its not nice but it works^^

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [December 9, 2010, 12:15pm UTC](https://discuss.rubyonrails.org/t/changing-tablename-without-changing-modelname/56158/8 "2010-12-09T12:15:13Z")

</div>

Fixtures are not nice even when they work. Maintenance is a nightmare. As soon as you have got it all working again then move over to Factories. The time you have wasted with this problem would have been enough to have made the switch.

Finally just a note about the changes you made, you said you adjusted the previous migrations so that running from scratch gave you the new table names. A better way would just have been to add a new migration that renamed the table.

Colin
