Teedub
2008-Dec-04 05:20 UTC
Is Rails "create_table" smart enough to know if a table already exists
If I run a migration and the table already exists, is rails
"create_table" smart enough to drop the table first, or should I add
something like line #1 below.
And..
if the foreign key ''fk_seats_venues'' existed, would I not need
to drop
it before dropping the table?
I am doing a bunch of experimental hacking with a couple projects so
the table might be there after I blow away schema.rb and drop the
schema table etc etc.
class CreateSeats < ActiveRecord::Migration
def self.up
execute "DROP TABLE IF EXISTS seats" #1
create_table(:seats, :id => false) do |t|
t.string :id, :limit => 36
t.integer :venue_id
t.string :name
t.string :section
t.string :seat_row
t.string :seat_number
t.string :seat_type
t.timestamps
end
execute "ALTER TABLE `seats` ADD PRIMARY KEY (`id`)"
#add a foreign key
execute "ALTER TABLE products ADD CONSTRAINT fk_seats_venues
FOREIGN KEY (category_id) REFERENCES venues(id)"
end
def self.down
#Drop foreign key
execute "ALTER TABLE products DROP FOREIGN KEY fk_seats_venues"
drop_table :seats
end
end
also, is there a way to find if the foreign key exists that less
complicated than:
IF EXISTS (SELECT NULL FROM information_schema.TABLE_CONSTRAINTS
WHERE CONSTRAINT_SCHEMA = DATABASE() AND CONSTRAINT_NAME
''fk_seats_venues'') THEN
ALTER TABLE `object` DROP FOREIGN KEY `fk_seats_venues`;
END IF;
and if not, how do I execute a multiline Sql statement?
Thanks in advance
TW Scannell
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Ryan Bigg
2008-Dec-04 05:31 UTC
Re: Is Rails "create_table" smart enough to know if a table already exists
there is an option for create_table called :force, specify it like: create_table :posts, :force => true do |t| ... end ----- Ryan Bigg Freelancer http://frozenplague.net On 04/12/2008, at 3:50 PM, Teedub wrote:> > > If I run a migration and the table already exists, is rails > "create_table" smart enough to drop the table first, or should I add > something like line #1 below. > And.. > if the foreign key ''fk_seats_venues'' existed, would I not need to drop > it before dropping the table? > > I am doing a bunch of experimental hacking with a couple projects so > the table might be there after I blow away schema.rb and drop the > schema table etc etc. > > class CreateSeats < ActiveRecord::Migration > def self.up > execute "DROP TABLE IF EXISTS seats" #1 > create_table(:seats, :id => false) do |t| > t.string :id, :limit => 36 > t.integer :venue_id > t.string :name > t.string :section > t.string :seat_row > t.string :seat_number > t.string :seat_type > > t.timestamps > end > execute "ALTER TABLE `seats` ADD PRIMARY KEY (`id`)" > > #add a foreign key > execute "ALTER TABLE products ADD CONSTRAINT fk_seats_venues > FOREIGN KEY (category_id) REFERENCES venues(id)" > > end > > def self.down > #Drop foreign key > execute "ALTER TABLE products DROP FOREIGN KEY fk_seats_venues" > drop_table :seats > end > end > > > also, is there a way to find if the foreign key exists that less > complicated than: > > > IF EXISTS (SELECT NULL FROM information_schema.TABLE_CONSTRAINTS > WHERE CONSTRAINT_SCHEMA = DATABASE() AND CONSTRAINT_NAME > ''fk_seats_venues'') THEN > ALTER TABLE `object` DROP FOREIGN KEY `fk_seats_venues`; > END IF; > > and if not, how do I execute a multiline Sql statement? > > Thanks in advance > > TW Scannell > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---