# From "schema.rb" file to MySQL

**URL:** <https://www.ruby-forum.com/t/from-schema-rb-file-to-mysql/137320>\
**Category:** Rails\
**Created:** [May 12, 2008, 12:11am UTC](https://www.ruby-forum.com/t/from-schema-rb-file-to-mysql/137320 "2008-05-12T00:11:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [May 12, 2008, 12:11am UTC](https://www.ruby-forum.com/t/from-schema-rb-file-to-mysql/137320/1 "2008-05-12T00:11:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [May 12, 2008, 12:25am UTC](https://www.ruby-forum.com/t/from-schema-rb-file-to-mysql/137320/2 "2008-05-12T00:25:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [May 12, 2008, 12:55am UTC](https://www.ruby-forum.com/t/from-schema-rb-file-to-mysql/137320/3 "2008-05-12T00:55: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

[http://rimantas.com/](http://rimantas.com/)

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [May 12, 2008, 3:26pm UTC](https://www.ruby-forum.com/t/from-schema-rb-file-to-mysql/137320/4 "2008-05-12T15: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 " 16 times is a good idea.

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [May 12, 2008, 1:21am UTC](https://www.ruby-forum.com/t/from-schema-rb-file-to-mysql/137320/5 "2008-05-12T01:21:10Z")

</div>

Rimantas L. 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:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [May 13, 2008, 6:40am UTC](https://www.ruby-forum.com/t/from-schema-rb-file-to-mysql/137320/6 "2008-05-13T06:40:39Z")

</div>

On Mon, May 12, 2008 at 11:29 PM, Greg D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [May 13, 2008, 6:01pm UTC](https://www.ruby-forum.com/t/from-schema-rb-file-to-mysql/137320/7 "2008-05-13T18:01:08Z")

</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

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [May 13, 2008, 6:30am UTC](https://www.ruby-forum.com/t/from-schema-rb-file-to-mysql/137320/8 "2008-05-13T06:30:06Z")

</div>

On Mon, May 12, 2008 at 8:26 AM, David B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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 ” 16 times is a good idea.

 

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

 

–  
Greg D.  
 [http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [May 13, 2008, 6:32pm UTC](https://www.ruby-forum.com/t/from-schema-rb-file-to-mysql/137320/9 "2008-05-13T18:32:23Z")

</div>

Thanks 🙂
