Bijan
Bijan

Reputation: 6772

How do define the schema that a rails model is set to?

For instance, when I generate an Event model, the table automatically sets to the public schema. How do I specify it to get set to a different schema?

Furthermore, how do you alter the schema of an existing table? Perhaps move it to a different schema?

Thank you!

Upvotes: 1

Views: 1075

Answers (2)

araqnid
araqnid

Reputation: 133482

Disclaimer: I don't know rails, so I'm going to give very postgresql-oriented answers here. For the first part of your question, there is quite possibly a much better way to do this, by making rails specify the schema when creating tables.

In PostgreSQL, tables are searched for in schemas according to the search_path setting. This is set by default to "$user",public. Tables are created in the first schema found in the search path that exists. So if you connect as "my_user", it will try to create tables in "my_user", and fall back to creating them in "public" if "my_user" doesn't exist.

So one approach is to update the "search_path" setting used for the user you connect to the database to make schema changes. For example you can say ALTER USER my_user SET search_path = my_app, public. If you then create a "my_app" schema then subsequent CREATE TABLE foo(...) commands executed by "my_user" will put the new table into "my_app".

You can change the schema of a table using ALTER TABLE foo SET SCHEMA my_app.

Upvotes: 1

shingara
shingara

Reputation: 46914

Create a migration to generate your new schema. ActiveRecord can't update you schema to you it's the pattern system. You can try sequel or DataMapper if you want update you schema from your code.

Upvotes: 0

Related Questions