Is Rails "create_table" smart enough to know if a table already exists


#1

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


#2

there is an option for create_table called :force, specify it like:

create_table :posts, :force => true do |t|

end

Ryan B.
Freelancer
http://frozenplague.net