question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get 'ER_TOO_LONG_KEY error running migrations in the utf8mb4 mysql database

See original GitHub issue

When running migrations, get the ER_TOO_LONG_KEY, probably because the mysql database charset is utf8mb4. How to make it work?

Here is the database config:

"staging": {
    "username": "USERNAME",
    "password": "PASSWORD",
    "database": "DATABASE",
    "host": "HOST",
    "dialect": "mysql",
    "port": 3306,
    "define": {
      "charset": "utf8mb4",
      "collate": "utf8mb4_general_ci"
    }
  }

Here are the logs:

Unhandled rejection SequelizeDatabaseError: ER_TOO_LONG_KEY: Specified key was too long; max key length is 767 bytes
    at Query.formatError (/root/www/smartChat-service-staging/node_modules/sequelize/lib/dialects/mysql/query.js:159:14)
    at Query._callback (/root/www/smartChat-service-staging/node_modules/sequelize/lib/dialects/mysql/query.js:35:21)
    at Query.Sequence.end (/root/www/smartChat-service-staging/node_modules/mysql/lib/protocol/sequences/Sequence.js:96:24)
    at Query.ErrorPacket (/root/www/smartChat-service-staging/node_modules/mysql/lib/protocol/sequences/Query.js:94:8)
    at Protocol._parsePacket (/root/www/smartChat-service-staging/node_modules/mysql/lib/protocol/Protocol.js:274:23)
    at Parser.write (/root/www/smartChat-service-staging/node_modules/mysql/lib/protocol/Parser.js:77:12)
    at Protocol.write (/root/www/smartChat-service-staging/node_modules/mysql/lib/protocol/Protocol.js:39:16)
    at Socket.<anonymous> (/root/www/smartChat-service-staging/node_modules/mysql/lib/Connection.js:96:28)
    at Socket.emit (events.js:95:17)
    at Socket.<anonymous> (_stream_readable.js:764:14)
    at Socket.emit (events.js:92:17)
    at emitReadable_ (_stream_readable.js:426:10)
    at emitReadable (_stream_readable.js:422:5)
    at readableAddChunk (_stream_readable.js:165:9)
    at Socket.Readable.push (_stream_readable.js:127:10)
    at TCP.onread (net.js:528:21)

Using version 3.4.1

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
apmcodescommented, May 24, 2019

Received the same error on a column with unique: true. Adding charset resolved the issue.

const sequelize = new Sequelize(connectionString, {
    dialect: 'mysql',
    logging: false,
    operatorsAliases: false,
    define: {
        charset: 'utf8',
        collate: 'utf8_general_ci',
    }
});
0reactions
DaggieBlanqxcommented, Jan 31, 2022

Received the same error on a column with unique: true. Adding charset resolved the issue.

const sequelize = new Sequelize(connectionString, {
    dialect: 'mysql',
    logging: false,
    operatorsAliases: false,
    define: {
        charset: 'utf8',
        collate: 'utf8_general_ci',
    }
});

This worked for me as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get 'ER_TOO_LONG_KEY error running migrations in the ...
When running migrations, get the ER_TOO_LONG_KEY, probably because the mysql database charset is utf8mb4. How to make it work?
Read more >
Migrate MySQL database to UTF8MB4 character encoding
Problem. When a user tries to insert a UTF8 character, which requires 4-byte encoding (e.g. emojis like " 😊🍻"), the “Incorrect String ...
Read more >
Migrating MySQL UTF8 to UTF8MB4 problems and questions
Is this a problem? What could I do to fix it? The next step is. ALTER TABLE table_name CHANGE column_name column_name VARCHAR( ...
Read more >
An in depth DBA's guide to migrating a MySQL database from ...
During the migration, with either utf8 or utf8mb4 connection settings, we'll find data belonging to the other charset. Is this a problem?
Read more >
Migrating Database Charsets to utf8mb4 - Percona
In this blog post, we'll look at options for migrating database charsets to utf8mb4.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found