SchemaPlus / schema_plus

SchemaPlus provides a collection of enhancements and extensions to ActiveRecord
Other
679 stars 84 forks source link

change_table tries to create a foreign key for polymorphic column #96

Open nagyt234 opened 11 years ago

nagyt234 commented 11 years ago

I'm usingRails 3.2.13 and schema_plus 1.1.2 . The following migration does'nt work:

class AddResourceToFileAssets < ActiveRecord::Migration
  def up
    change_table :file_assets do |t|
      t.references :resource, :polymorphic => true
    end
  end

  def down
  end
end

schema_plus tries to create a foreign key pointing to a non-existent table:

==  AddResourceToFileAssets: migrating ========================================
-- change_table(:file_assets)
rake aborted!
An error has occurred, all later migrations canceled:
Mysql2::Error: Can't create table 'v2p0_development.#sql-70e_360' (errno: 150): ALTER TABLE `file_assets` ADD CONSTRAINT fk_file_assets_resource_id FOREIGN KEY (`resource_id`) REFERENCES `resources` (`id`)/usr/local/rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `query'
/

This seems to be a bug. Note that the same syntax works correctly with create_table, it was fixed in 0.4.1.

nagyt234 commented 11 years ago

Workaround:

t.references :resource, :polymorphic => true, foreign_key: false

AlfonsoUceda commented 11 years ago

I'm in rails 3.0.20 with ruby 1.9.3, the version is 0.4.1 and the bug still persists.

I'm using the change_table method