Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

config.active_record.table_name_prefix affects migrations even if table_name is set #6617

Closed
denispeplin opened this Issue · 7 comments

5 participants

@denispeplin

In my application.rb

config.active_record.table_name_prefix = 'prefix_'

in user.rb

class User < ActiveRecord::Base
  self.table_name = 'admin_users'
end

in migration file:

class AddDefaultProviderIdToUsers < ActiveRecord::Migration
  def change
    add_column :admin_users, :default_provider_id, :integer
  end
end

rake db:migrate output:

PG::Error: ERROR:  relation "prefix_admin_users" does not exist
: ALTER TABLE "prefix_admin_users" ADD COLUMN "default_provider_id" integer

So the problem is, that migration do not respect table_name setting in User model, but auto-prepend table names with default prefix, so I can't set table name from migration. Only way to perform this migration is to do it manually.

Rails version is 3.2.5

@rafaelfranca
Owner

Related with #4259 and #4396

@parndt

I don't see how the migration could know not to put the prefix on without loading the model in question? I would think that it could respect this when you're migrating with but only in this case:

class AddDefaultProviderIdToUsers < ActiveRecord::Migration
  def change
    add_column User.table_name, :default_provider_id, :integer
  end
end

Not that I'd recommend using .table_name in a migration.

What do you think?

@senny
Owner

I agree with @parndt. Without loading the model there is no way we can know that self.table_name was set in the model. We don't want to link the migrations to the model because this leads to trouble when the model evolves but the migrations must stay the same.

I think we should simply provide a way to not prepend the prefix.

@carlosantoniodasilva @rafaelfranca let me know what you think and I'll submit a PR.

@rafaelfranca

@senny is not the current behaviour ignoring the prefix? See #4396.

@senny
Owner

@rafaelfranca completely ignoring the prefix in migrations is also a solution. I'm not sure though what the current code is doing. I just skimmed the code and thought it's passing through:

https://github.com/rails/rails/blob/master/activerecord/lib/active_record/migration.rb#L590-L591

@rafaelfranca

I don't think the migrator is ignoring the prefix. But I don't think it should. What is the point of the prefix and sufix if they will be ignored?

@laurocaetano
Collaborator

Well, if you want to use a global table name prefix and ignore in this case, you could just set the self.table_name_prefix to be an empty string. Just like:

class User < ActiveRecord::Base
  self.table_name_prefix = ''
  self.table_name = 'admin_users'
end

I agree with @rafaelfranca, there is no point of using prefix/sufix and ignoring them. Closing this since it seems to be an edge case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.