Question: Migration - multiple creates


#1

I want to create a migration file that creates multiple tables, so i’m
guessing the correct format would be:
Can anyone confirm ? I’m guessing that each new create script requires a
seperate class so formatted this way. Perhaps I’m wrong.

TIA
Stuart

class CreateTable1 < ActiveRecord::Migration
def self.up
create_table :table1s do |t|
t.column :length, :string
end

end

def self.down
drop_table :table1s
end
end

class CreateTable2 < ActiveRecord::Migration
def self.up
create_table :table2s do |t|
t.column :length, :string
end

end

def self.down
drop_table :table2s
end
end

class CreateTable3 < ActiveRecord::Migration
def self.up
create_table :table3s do |t|
t.column :length, :string
end

end

def self.down
drop_table :table3s
end
end


#2

Maybe you should try it and see?

I can’t help you because I honestly don’t know, but I do know how I
would proceed.

Dark A. wrote:

I want to create a migration file that creates multiple tables, so i’m
guessing the correct format would be:


#3

It didn’t work.

Stuart


#4

Actually only the first table was created.

Stuart


#5

On 6/13/06, Dark A. removed_email_address@domain.invalid wrote:

Actually only the first table was created.

You can put multiple table definition blocks into a single class
declaration. When the migration is called, it only calls the
migration class that corresponds to the migration file name…
Remember, convention over configuration. :wink:

-Curtis


#6

You can only have one set of self.up/self.down methods in a migration
class, and only one class per file, but you can create, destroy, or
modify multiple tables in each up and down. So, you’d want something
like:

class CreateTables < ActiveRecord::Migration
def self.up
create_table :table1s do |t|
t.column :length, :string
end
create_table :table2s do |t|
t.column :length, :string
end
create_table :table3s do |t|
t.column :length, :string
end
end

def self.down
drop_table :table1s
drop_table :table2s
drop_table :table3s
end
end


#7

Stop the presses ! I got it to work. Don’t ask how :slight_smile: Format is the
same.

Thank you,
Stuart


#8

Jay,
Wasn’t sure if I thanked you for the help, so thank you!

Stuart


#9

I ran into a migration snag again. I’ve googled around and come up with
a
few things but none that specifically show my scenario. I’m tryint to
migrate data into multiple tables at once.

Tried a few different ways with no success -
I get this error:

:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:123:in
`rake_original_const_missing’: uninitialized constant Postlength
(NameError)

Postlength.create = Postlength is the singular name of the table
postlengths
:postlength is the column name in the table.

This is part of the file:

class Addstaticdata < ActiveRecord::Migration
def self.up
Postlength.create(:postlength => ‘7 Days’)
Postlength.create(:postlength => ‘14 Days’)
Postlength.create(:postlength => ‘21 Days’)
Postlength.create(:postlength => ‘30 Days’)
Postlength.create(:postlength => ‘45 Days’)
Postlength.create(:postlength => ‘60 Days’)
Edureq.create(:edureq => ‘PhD’)
Edureq.create(:edureq => ‘Juris Doctorate’)
Edureq.create(:edureq => ‘Trade School’)
Contactmethod.create(:contactmethod => ‘Direct through details’)
Contactmethod.create(:contactmethod => ‘Through website’)
Contactmethod.create(:contactmethod => ‘Both direct and
website’)
Securityclear.create(:securityclear => ‘None’)
Securityclear.create(:securityclear => ‘Active Confidential’)
Securityclear.create(:securityclear => ‘Active Top Secret’)
Securityclear.create(:securityclear => ‘Active Top Secret SCI’)
end

def self.down
end
end

Hope I’m not being a pain here.
Stuart


#10

Have you created a Postlength model yet? You’ll need that to populate
your table.


#11

Ahh, okay Jay …thank you. I was thinking about that possibly being the
cause but left that out of my experiments.
Thank you again, I owe you !

Stuart


#12

Jay, thanks actually right before your email came through that is what I
had
come up with:
Only it’s not working, not coughing errors but not working. I usually
run
through the IDE RadRails, and received just a

(in C:/InstantRails/rails_apps/lfw)

So I went into the rails console (script/console) and I guess that might
have not been a good idea. I ran rake migrate and got back:

** Invoke migrate (first_time)
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:migrate
** Invoke db:schema:dump (first_time)
** Invoke environment
** Execute db:schema:dump
** Execute migrate

Yet, this was not the first time I ran rake migrate. So I tried it
again in
the cosole but added --trace and got back:

(in C:/InstantRails/rails_apps/lfw)

No tables were created though.

Here is the migration file I created.

class CreateTables < ActiveRecord::Migration
def self.up
create_table :postlengths do |t|
t.column :postlength, :string
end
create_table :expreqs do |t|
t.column :expreq, :string
end
create_table :edureqs do |t|
t.column :edureq, :string
end
create_table :contactmethods do |t|
t.column :contactmethod, :string
end
create_table :securityclears do |t|
t.column :securityclear, :string
end
end

def self.down
drop_table :postlengths
drop_table :expreqs
drop_table :edureqs
drop_table :contactmethods
drop_table :securityclears
end
end

Not sure what’s going on.

Stuart