In my Grails domain i currently have
String value
static constraints = {
value(blank: false, size: 1..400)
}
static mapping = {
value index: 'value_Idx'
}
My datasource is MySQL InnoDB which has a maximum index length of 767 bytes.
Using UTF-8 this means maximum 255 characters which in turn means that a prefix index must be used.
In MySQL <= 5.5 I receive no error from Grails and when looking at the resulting index the prefix is silently adjusted to 255:
mysql> show index from table;
+--------+-------------+--------+----------+--------+
| .... | Column_name | .... | Sub_part | .... |
+--------+-------------+--------+----------+--------+
| .... | value | .... | 255 | ..... |
+--------+-------------+--------+----------+--------+
In MySQL 5.6 I receive an error during Grails application startup:
ERROR hbm2ddl.SchemaUpdate - Unsuccessful: create index value_Idx on table (value)
ERROR hbm2ddl.SchemaUpdate - Index column size too large. The maximum column size is 767 bytes.
This is explained in the documentation that states that:
If you specify an index prefix length that is greater than the allowed
maximum value, the length is silently reduced to the maximum length.
In MySQL 5.6 and later, specifying an index prefix length greater than
the maximum length produces an error.
To support all versions of MySQL i which to specify the prefix index length explicitly in my domain class mapping. Is this possible? and how is this implemented?
I know how to do it in MySQL console but not how to do the similar in Grails/Gorm/Hibernate.
Stepped on the same problem recently.
We used Grails Database Migration Plugin to archive that. Follow the installation steps provided in the docs and then create a new changelog grails-app/migrations/add-prefixed-index.groovy
for example:
databaseChangeLog = {
changeSet(author: "authot", id: "addPrefixedIndex") {
grailsChange {
change {
sql.execute('ALTER TABLE `table` ADD INDEX `value_Idx`(`value`(255));')
}
rollback {
sql.execute('ALTER TABLE `table` DROP INDEX `value_Idx`;')
}
}
}
}
Don’t forget to include your new file in the main database changelog: include file: 'add-prefixed-index.groovy'
Then run grails dbm-update
and your new index is created.