Search code examples
ruby-on-railsindexingactiverecordmigrationsharding

Rails migration index name for partitioned database table is too long


My Rails application's database is partitioned into shards, with partition_key of each shard being toyota_seller_account_id. Now I want to create a table called agg_product_summary_breakdowns using this migration:

class CreateAggProductSummaryBreakdowns < ActiveRecord::Migration[5.2]
  def change
    create_table :agg_product_summary_breakdowns, partition_key: :toyota_seller_account_id do |t|
      t.references :toyota_seller_account, null: false
      t.date :summary_date, null: false
      t.timestamps
    end
  end

  def down
    drop_table :agg_product_summary_breakdowns
  end
end

Now Rails will automatically create an index on toyota_seller_account_id with name index_agg_product_summary_breakdowns_on_toyota_seller_account_id because it is the partition key.

However, I will then encounter this error:

ArgumentError: Index name 'index_agg_product_summary_breakdowns_on_toyota_seller_account_id' on table 'agg_product_summary_breakdowns' is too long; the limit is 63 characters. Unfortunately it is just 64 characters long (1 longer than what's permitted).

I want to keep the table name as it is, i.e. agg_product_summary_breakdowns and the partition key cannot be changed either. Is there a way to override the index name that I'm not aware of?

Thanks!


Solution

  • Just change the name of the index. Try with:

    t.references :toyota_seller_account, null: false, index: {name: :whatever_you_want}