How to programmatically create a limited length MySQL index using JPA?

Like here I annotate my class using

@Table(indexes = {@Index(columnList = "name")}) 

which is trying to create an imperfect index with a maximum column length varchar. Unfortunately, this is not possible because it is a varchar(255)type column utf8mb4. phpMyAdmin added KEY '...' (name(191))by clicking on the appropriate buttons in the user interface, so at least my software now runs efficient queries.

Now I was wondering if my Java class can automatically generate a limited length index when creating a database schema? The code is built on spring-boot-starter-data-jpa:1.4.2.RELEASE.

+4
source share
2 answers

There are other answers than trying to get third-party software to do something that it may or may not allow.

  • Live with a limit of 191 for column size. Or do you really have a max between 191 and 255.
  • Change to utf8 (with utf8mb4). And lose the ability to store Emoji and some Chinese characters.
  • There is a clumsy process in 5.6 to raise the limit of 767 that you come across.
  • Update to 5.7, which actually fixes the problem.
+2
source

Only JPA table scripts should be used as a starting point, and you should not use JPA to create tables during production.

"create table", DBA , Flyway . XML, LiquideBase.

script , . JPA script, , , , , , varchar(255) 255 , , - , 1.

Flyway Spring, , , ( ) script src/main/resources/db/migration/V1__initial_script.sql.

+2

Source: https://habr.com/ru/post/1670398/


All Articles