Dear nodejs experts and database gurus,
We face issues with storing emojis and other special characters in our MySQL database. We get an error by Prisma, which is the "ORM" we use:
Conversion from collation utf8_general_ci into utf8mb4_unicode_520_ci impossible for parameter
prisma:query INSERT INTO `app`.`show` [...]
PrismaClientUnknownRequestError: Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind: QueryError(Server(ServerError {
code: 3988,
message: "Conversion from collation utf8_general_ci into utf8mb4_unicode_520_ci impossible for parameter",
state: "HY000" })) })
at cb (/app/node_modules/@prisma/client/runtime/index.js:38695:17) { clientVersion: '3.8.1' }
prisma:query ROLLBACK
We tried the following solutions already:
- Using different collations on the database (db level, table level, field level):
utf8mb4_unicode_520_ci
,utf8mb4_unicode_ci
, but the goal is to use a set which is most modern likeutf8mb4_0900_ai_ci
- Changing it to utf8 instead of utf8mb4 resulted in an error like
Incorrect string value: '\\xF0\\x9F\\x92\\x95'
and is not our goal as we want to be able to store all emojis - Dumping, changing all encoding statements in the sql dump and re-importing with parameters. We used a bunch of parameters in hope it will encode it differently like
--default-character-set=utf8mb4
or--hex-blob
- Defining a charset in our backend as we believe that the database is not the issue, but more the input we send to it like the old PHP way we knew
ini_set("default_charset", "UTF-8")
, but as we understood Nodejs does not care about the encoding/charset, but maybe there is a way - Defining a charset at the MySQL client, but we did not find a way to do so as we use prisma.io and maybe do not have access to the mysql client underhood. Prisma by itself seems to support utf8mb4 as this is in the docs https://www.prisma.io/docs/concepts/components/prisma-client/case-sensitivity#database-collation-and-case-sensitivity and we found a recent discussion about defining it on a more granular level here https://github.com/prisma/prisma/discussions/4743
Maybe two more valuable insights:
- The data comes from a React Native app, but before the database migration, we had no issues, so we hope it is not a problem on the client-side, but for sure, we can specify something there as well, eg. setting HEADER information on the API call, which we do with
axios
- Before we used the db-as-a-service tool
Airtable
, which was quite a hassle to "break free" from them. We exported everything in CSV format and imported it in a local MySQL db powered withXAMPP
(which had different options for encoding) and then imported the dump to our new MySQL cluster
Our goal is to
- Set the right encoding character set in our new MySQL cluster
- Send the data in the right format to the DB (setting the same set in NodeJS and React)
Hope you can help us! Thanks!