# Migrations

**URL:** <https://www.ruby-forum.com/t/migrations/80414>\
**Category:** Rails\
**Created:** [December 29, 2005, 3:18pm UTC](https://www.ruby-forum.com/t/migrations/80414 "2005-12-29T15:18:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![0ffh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/0/ea666f/32.png) [@0ffh](https://www.ruby-forum.com/u/0ffh)\
**Post date:** [December 29, 2005, 3:18pm UTC](https://www.ruby-forum.com/t/migrations/80414/1 "2005-12-29T15:18:13Z")

</div>

I’m trying to understand migrations and here is my problem, lets take  
this example code from the API docs for migration. This code creates a  
table then adds a new row to it, my problem is how can this add a row  
to the table if the model doesn’t exist and how can you create a model  
if the table doesn’t first exist. Can the migrations also be told to  
create a model for a new table? Because right now if I run this code  
it will fail to add the first row because the model does not exist  
yet.

class AddSystemSettings \< ActiveRecord::Migration  
def self.up  
create\_table :system\_settings do |t|  
t.column :name, :string  
t.column :label, :string  
t.column :value, :text  
t.column :type, :string  
t.column :position, :integer  
end

```
  SystemSetting.create :name => "notice", :label => "Use notice?",

```

:value =\> 1  
end

```
def self.down
  drop_table :system_settings
end

```

end

---

<div class="post-metadata">

**Author:** ![0ffh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/0/ea666f/32.png) [@0ffh](https://www.ruby-forum.com/u/0ffh)\
**Post date:** [December 29, 2005, 3:39pm UTC](https://www.ruby-forum.com/t/migrations/80414/2 "2005-12-29T15:39:30Z")

</div>

On Dec 29, 2005, at 7:17 AM, Frank H. wrote:

> I’m trying to understand migrations and here is my problem, lets take  
> this example code from the API docs for migration. This code creates a  
> table then adds a new row to it, my problem is how can this add a row  
> to the table if the model doesn’t exist and how can you create a model  
> if the table doesn’t first exist…

The table does not need to exist in order to define a new model–it  
only needs to exist the first time you try to _use_ the model. Thus,  
you can use the generator to create a new model, and then reference  
the model in the migration that creates the table.

- Jamis

---

<div class="post-metadata">

**Author:** ![0ffh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/0/ea666f/32.png) [@0ffh](https://www.ruby-forum.com/u/0ffh)\
**Post date:** [December 29, 2005, 3:42pm UTC](https://www.ruby-forum.com/t/migrations/80414/3 "2005-12-29T15:42:32Z")

</div>

Jamis B. wrote:

> The table does not need to exist in order to define a new model–it  
> only needs to exist the first time you try to _use_ the model. Thus,  
> you can use the generator to create a new model, and then reference the  
> model in the migration that creates the table.

On a related note, is there a clean way to call the scaffold generator  
(or any generator, for that matter) from within a migration? The  
scaffold generator won’t fly without a table. I know it’s not what the  
migrations are supposed to be for, but it’d be damn nice to have 🙂

---

<div class="post-metadata">

**Author:** ![0ffh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/0/ea666f/32.png) [@0ffh](https://www.ruby-forum.com/u/0ffh)\
**Post date:** [December 29, 2005, 3:48pm UTC](https://www.ruby-forum.com/t/migrations/80414/4 "2005-12-29T15:48:40Z")

</div>

I was incorrectly assuming you couldn’t generate a model for a table  
that did not exist in the DB yet. I was thinking of what Alex was  
talking about. Sometimes these things can get confusing (well for at  
least me, haha). Thanks for the help.
