# From "schema.rb" file to MySQL

**URL:** <https://discuss.rubyonrails.org/t/from-schema-rb-file-to-mysql/29236>\
**Category:** rubyonrails-talk\
**Created:** [May 11, 2008, 10:11pm UTC](https://discuss.rubyonrails.org/t/from-schema-rb-file-to-mysql/29236 "2008-05-11T22:11:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [May 11, 2008, 10:11pm UTC](https://discuss.rubyonrails.org/t/from-schema-rb-file-to-mysql/29236/1 "2008-05-11T22:11:43Z")

</div>

Hello,

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

ActiveRecord::Schema.define do &nbsp;&nbsp;create\_table :arts do |t| &nbsp;&nbsp;&nbsp;&nbsp;t.string :name, :null =\> false, :limit =\> 45 &nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [May 11, 2008, 10:25pm UTC](https://discuss.rubyonrails.org/t/from-schema-rb-file-to-mysql/29236/2 "2008-05-11T22:25:45Z")

</div>

David B. wrote:

> Hello,
> 
> I have write a schema.rb database such as this:
> 
> ActiveRecord::Schema.define do &nbsp;&nbsp;create\_table :arts do |t| &nbsp;&nbsp;&nbsp;&nbsp;t.string :name, :null =\> false, :limit =\> 45 &nbsp;&nbsp;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.

---

<div class="post-metadata">

**Author:** ![Rimantas\_Liubertas](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@Rimantas\_Liubertas](https://discuss.rubyonrails.org/u/Rimantas_Liubertas)\
**Post date:** [May 11, 2008, 10:54pm UTC](https://discuss.rubyonrails.org/t/from-schema-rb-file-to-mysql/29236/3 "2008-05-11T22:54:14Z")

</div>

\<...\>

> 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

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [May 11, 2008, 11:21pm UTC](https://discuss.rubyonrails.org/t/from-schema-rb-file-to-mysql/29236/4 "2008-05-11T23:21:10Z")

</div>

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/](http://rimantas.com/)

Wow! This command work perfectly, thanks a lot! 🙂

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [May 12, 2008, 1:26pm UTC](https://discuss.rubyonrails.org/t/from-schema-rb-file-to-mysql/29236/5 "2008-05-12T13:26:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Greg\_Donald1](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/greg_donald1/32/1313_2.png) [@Greg\_Donald1](https://discuss.rubyonrails.org/u/Greg_Donald1)\
**Post date:** [May 13, 2008, 4:29am UTC](https://discuss.rubyonrails.org/t/from-schema-rb-file-to-mysql/29236/6 "2008-05-13T04:29:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Greg\_Donald1](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/greg_donald1/32/1313_2.png) [@Greg\_Donald1](https://discuss.rubyonrails.org/u/Greg_Donald1)\
**Post date:** [May 13, 2008, 4:40am UTC](https://discuss.rubyonrails.org/t/from-schema-rb-file-to-mysql/29236/7 "2008-05-13T04:40:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kyle3](https://avatars.discourse-cdn.com/v4/letter/k/54ee81/32.png) [@Kyle3](https://discuss.rubyonrails.org/u/Kyle3)\
**Post date:** [May 13, 2008, 4:00pm UTC](https://discuss.rubyonrails.org/t/from-schema-rb-file-to-mysql/29236/8 "2008-05-13T16:00:11Z")

</div>

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

-Kyle
