Oskari
Oskari

Reputation: 11

Changing table name in SQLite3 using Rails?

I have a SQLite3 database and I changed a table called People to Persons (to better match the standard of having a singular Person model).

I made this change manually in the database using SQLite3 and the call:

ALTER TABLE People RENAME TO Persons;

Now when I run the app I get an error:

SQLite3::SQLException: no such table: people: SELECT "people".* FROM "people"

I changed the table name in the controller, model, migration and schema files to match the change. Where else should I make the change?

Upvotes: 1

Views: 234

Answers (2)

xdazz
xdazz

Reputation: 160833

If you change the table name, then Rails' default convention is broken. You have to tell Rails to use the persons table instead of people because Rails thought the table for Person model is people.

[1] pry(main)> 'person'.pluralize
=> "people"

If you still want to change the table name, then you need to config it like:

class Person < ActiveRecord::Base
    self.table_name = "persons"
end

Upvotes: 2

nktokyo
nktokyo

Reputation: 642

I think you would be better generating a model "person" and letting Rails handle the pluralization. It probably converts to people and is getting confused.

You would then migrate data over and then drop your old table. You would have to check the naming for your views and controllers also.

Upvotes: 2

Related Questions