Forum: Ruby on Rails Is Rails "create_table" smart enough to know if a table already exists

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D3fe2dc4435cdd79e58d3e1380a557a1?d=identicon&s=25 Teedub (Guest)
on 2008-12-04 06:21
(Received via mailing list)
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
9a2a53db8e9b4476038c94a64b32833f?d=identicon&s=25 Ryan Bigg (ryan-bigg)
on 2008-12-04 06:31
(Received via mailing list)
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
This topic is locked and can not be replied to.