Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Foreign key creation fails when a table prefix or suffix is used #19749

Conversation

arjes
Copy link

@arjes arjes commented Apr 13, 2015

Simply using a prefix, or suffix:

  config.active_record.table_name_prefix = 'test_'

will break the addition of a foreign key with the following migration:

class Test < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.timestamps
    end

    create_table :comments do |t|
      t.integer :post_id
      t.timestamps
    end
    add_foreign_key :comments, :posts
  end
end

This migration attempts to add a foreign key referencing the test_post_id column. If you specify the column the migration will run correctly.

In order to correct this I extracted the proper_table_name and the remove_prefix_and_suffix function to the abstract connector since it is needed in multiple contexts (SchemaStatments and Migrations)

@arjes
Copy link
Author

arjes commented Apr 13, 2015

This spec isn't failing locally, reopening.

@arjes arjes closed this Apr 13, 2015
@arjes arjes reopened this Apr 13, 2015
@@ -796,7 +796,7 @@ def remove_foreign_key(from_table, options_or_to_table = {})
end

def foreign_key_column_for(table_name) # :nodoc:
"#{table_name.to_s.singularize}_id"
"#{ActiveRecord::Base.connection.remove_prefix_and_suffix(table_name).to_s.singularize}_id"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not self.class here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sgrif I didn't realize I was in that context here, I'll make the update.

@sgrif
Copy link
Contributor

sgrif commented Apr 14, 2015

Please squash into a single commit

@sgrif
Copy link
Contributor

sgrif commented Apr 14, 2015

/cc @senny

@arjes
Copy link
Author

arjes commented Apr 14, 2015

@sgrif I'd like some more feedback on a few of your comments before I re-submit with the updates. Thanks for the feedback.

@arjes arjes force-pushed the correct_foreign_keys_with_prefix_and_suffix branch from 047ce9e to c7457d0 Compare April 21, 2015 19:16
When using a prefix of suffix with AR, the add_foreign_key migrator
would fail to properly identify the destination column properly.

i.e.
  ActiveRecord::Base.table_name_prefix = "foo_"

  add_foreign_key :comments, :posts
  => Attempts to create a foreign key on the column name foo_posts_id

Since this code is used in multiple places, the function
proper_table_name was extracted to the abstract_adapter.
@arjes arjes force-pushed the correct_foreign_keys_with_prefix_and_suffix branch from c7457d0 to eca2484 Compare April 21, 2015 19:19
@arjes
Copy link
Author

arjes commented Apr 21, 2015

Squashed & Rebased. Waiting for feedback on remaining items.

ActiveRecord::Base.table_name_suffix = ''
end

def test_add_foreign_key_with_prefix_and_suffix
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_remove_foreign_key_with_prefix_and_suffix and test_add_foreign_key_with_prefix_and_suffix are very similar. It's probably worth combining them. Then we can add and remove the foreign key in a single test. Something along the lines of

test_managing_foreign_keys_with_prefix_and_suffix

@sgrif
Copy link
Contributor

sgrif commented Oct 20, 2015

Sorry to close after all of this, but I just realized this is against 4-2-stable. Can you re-open against master?

@sgrif sgrif closed this Oct 20, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants