D
D
Dmitry2015-09-02 00:28:18
MySQL
Dmitry, 2015-09-02 00:28:18

Error creating index in Mysql2 database - how to specify the length of a composite key?

I must say right away that there is no error in Sqlite.
I decided to add the "rolify" gem as a permission delimiter in conjunction with devise and cancancan. Everything was OK in the development environment, but when I uploaded it to the server, an error was found:

-- add_index("roles", ["name", "resource_type", "resource_id"], {:name=>"index_roles_on_name_and_resource_type_and_resource_id"})
rake aborted!
ActiveRecord::StatementInvalid: Mysql2::Error: Specified key was too long; max key length is 1000 bytes: CREATE  INDEX `index_roles_on_name_and_resource_type_and_resource_id`  ON `roles` (`name`, `resource_type`, `resource_id`)

As it turned out, a MyISAM-type database has a limit of 1000 bytes, which, taking into account UTF8, yields ~ 333 characters. Therefore, the solution should have been:
add length: {name:100, resource_type:20, resource_id:11}
but alas, this parameter is ignored! Cm:
-- add_index("roles", ["name", "resource_type", "resource_id"], {:name=>"index_roles_on_name_and_resource_type_and_resource_id", :length=>{:name=>100, :resource_type=>20, :resource_id=>11}})
rake aborted!
ActiveRecord::StatementInvalid: Mysql2::Error: Specified key was too long; max key length is 1000 bytes: CREATE  INDEX `index_roles_on_name_and_resource_type_and_resource_id`  ON `roles` (`name`, `resource_type`, `resource_id`)

The logical solution was to write:
#execute "CREATE  INDEX `index_roles_on_name_and_resource_type_and_resource_id`  ON `roles` (`name`(100), `resource_type`(20), `resource_id`(11))"

But it doesn't work in development environment. What to do with it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Wolf, 2015-09-02
@mannaro

What version of Ruby (and RoR)?
Example from APIDoc:

add_index(:accounts, [:name, :surname], name: 'by_name_surname', length: {name: 10, surname: 15})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question