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.

Typeorm not using default charset from database

See original GitHub issue

Issue type:

[ ] question [ x ] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ x ] mysql / mariadb [ ] oracle [ ] postgres [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[ x ] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

From MySQL connection options:

/**
 * The charset for the connection. This is called "collation" in the SQL-level of MySQL (like utf8_general_ci).
 * If a SQL-level charset is specified (like utf8mb4) then the default collation for that charset is used.
 * Default: 'UTF8_GENERAL_CI'
 */
readonly charset?: string;

Using the SQL-level charset seems not to work properly. My MySQL database is configured as:

Default collation: utf8mb4_unicode_ci
Default charset: utf8mb4

When inserting a string, that contains an emoji, the query fails with: ER_TRUNCATED_WRONG_VALUE_FOR_FIELD, 'Incorrect string value: \'\\xF0\\x9F\\x98\\x9C \' for column \'message\' at row 1',

That indicates, that the default charset of the database is not used, and the default UTF8_GENERAL_CI used instead (which does not support emoji). And in deed, if I set charset: 'utf8mb4' explicitly in the typeorm config, inserting works properly.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
woodinteractivecommented, Mar 10, 2021

charset: ‘utf8mb4’

it works. typeorm documentation is tricky because they only put the general stuffs in their webpage, but more specific things are in their github repo

@robertoandres24 Can you clarify what exactly worked? I’m having a similar issue and my column types are set to ‘uft8mb4’, but I’m still getting the error message ER_TRUNCATED_WRONG_VALUE_FOR_FIELD: Incorrect string value: '\xE2\x80\x90tim...' for column 'content' at row 1

0reactions
zw-slimecommented, Sep 29, 2022

My solution is

  imports: [
    CoreModule,
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: config.mysql.host,
      port: config.mysql.port,
      username: config.mysql.username,
      password: config.mysql.password,
      database: config.mysql.database,
      entities: [PageNewEntity, TaskNewEntity],
      synchronize: true,
      charset:'utf8mb4', // set this  set Column set entity do not work
      logging: false,
      // timezone: config.mysql.timezone,
    })
  ],
  exports: [TaskNewModule, TaskAsyncModule],
})
export class AppModule {}```
Read more comments on GitHub >

github_iconTop Results From Across the Web

mysql - Encoding not rendering correctly - Stack Overflow
So far, so good. I'm using TypeORM to retrieve my information (note : the database already existed before implementing TypeORM), I configured it ......
Read more >
Entities - typeorm - GitBook
Entity is a class that maps to a database table (or collection when using ... If the @DeleteDateColumn is set, the default scope...
Read more >
MysqlConnectionOptions - typeorm
Connection name. If connection name is not given then it will be called "default". Different connections must have different names.
Read more >
MysqlConnectionOptions | typeorm
If a SQL-level charset is specified (like utf8mb4) then the default collation for ... Naming strategy to be used to name tables and...
Read more >
Decorator reference | TypeORM Docs
When set to false , the column data will not show with a standard query. By default column is select: true; default: string...
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