TwilightCoders / active_record-mti

ActiveRecord support for PostgreSQL's native inherited tables (multi-table inheritance)
MIT License
98 stars 7 forks source link

inheriting indexes can get a > 64 character limit error #3

Open ghost opened 7 years ago

ghost commented 7 years ago

Issue: When mti adds indexes to children tables it can fail if the generated index name is too long. Rails' add_index function generates the index name by default using the table name and the column your indexing (or array of columns if it's a compound index). If this constructed table name is > 64 characters then add_index will fail.

One thing to note is the :name attribute isn't being passed to add_index when constructing the child index in "active_record-mti/lib/active_record/mti/connection_adapters/postgresql/schema_statements.rb #create_table"

Steps to Reproduce: Add the following to the spec/schema.rb file and try to run tests.

create_table :vehicles, force: true do |t|
    t.string :color
    t.string :type # Inheritance column
    t.timestamps null: false
  end

  add_index :vehicles, [:color, :type]

  create_table "vehicles/trucks", force: true, inherits: :vehicles do |t|
    t.integer :bed_size
  end

  # different schema of parent
  create_table "vehicles/some_really_really_really_really_long_table_name", force: true, inherits: :vehicles do |t|
    t.integer :long_size
  end

  # same schema of parent
  create_table "vehicles/some_other_really_really_really_long_table_name", force: true, inherits: :vehicles do |t|
end

Expected Result: It creates the compound indexes on the children tables.

Actual Result: The following error is thrown: _Index name 'index_vehicles/some_really_really_really_really_long_table_name_on_color_and_type' on table 'vehicles/some_really_really_really_really_long_tablename' is too long; the limit is 63 characters (ArgumentError)