From "schema.rb" file to MySQL

Hello,

I have write a schema.rb database such as this:

ActiveRecord::Schema.define do   create_table :arts do |t|     t.string :name, :null => false, :limit => 45   end end

This file is saved into test/db/schema.rb, and test/config/database.yml is correctly configured with "test_development" database created in a MySQL server.

Could you help me to install the content of schema.rb file in MySQL and in my Rails 2 application?

Thank you for your help.

Regards

David B. wrote:

Hello,

I have write a schema.rb database such as this:

ActiveRecord::Schema.define do   create_table :arts do |t|     t.string :name, :null => false, :limit => 45   end end

This file is saved into test/db/schema.rb, and test/config/database.yml is correctly configured with "test_development" database created in a MySQL server.

Well, this is a rather awkward way of doing things. Effectively, db/schema.rb is a composite migration file. To load it one would typically move it to something called similar to db/migrate/001_initial_db_load.rb and run rake db:migrate. When one runs the rake task, depending upon the settings in config/environment.rb, then db/schema.rb gets overwritten with the results of the applied migrations, making direct editing of this file rather pointless.

It would be far better for you to read up on migrations and to use individual migrations contained in db/migrate instead.

<...>

Well, this is a rather awkward way of doing things. Effectively, db/schema.rb is a composite migration file. To load it one would typically move it to something called similar to db/migrate/001_initial_db_load.rb and run rake db:migrate.

To load schema.rb. you do not need to move or rename anything, just use rake db:schema:load

Regards, Rimantas

Rimantas Liubertas wrote:

<...>

Well, this is a rather awkward way of doing things. Effectively, db/schema.rb is a composite migration file. To load it one would typically move it to something called similar to db/migrate/001_initial_db_load.rb and run rake db:migrate.

To load schema.rb. you do not need to move or rename anything, just use rake db:schema:load

Regards, Rimantas -- http://rimantas.com/

Wow! This command work perfectly, thanks a lot! :slight_smile:

By the way, I just have a last question: is there a command to automaticly create all the model for any tables of my database? I ask this because my database have 16 tables and I don't think using "script/generate model <a table>" 16 times is a good idea.

for x in `echo "show tables" | mysql my_db`; do script/generate model $x; done

Oops, I forgot to remove the 's' from the table name, model names usually being singular and all:

for x in `echo "show tables" | mysql my_db | sed 's/s$//'`; do script/generate model $x; done

That should work well as long as you don't have a singular/plural pair that doesn't use "+s" for the plural (e.g., pony, ponies). You could probably route your command through pluralize, but in that case, it would be easier just to type in the 16 commands. :slight_smile:

-Kyle