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.

Cannot change charset of MySQL database tables

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

TypeORM version:

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

Steps to reproduce or a small repository showing the problem:

Trying to solve another issue, I cannot seem to create a basic table in a database with a default charset of UTF8_GENERAL_CI (which I understood should be default, unless I got it wrong, which means the docs are blurry).

Here’s my connection:

createConnection({
    type: "mysql",
    host: process.env.DB_HOST,
    port: parseInt(process.env.DB_PORT || ''),
    username: process.env.DB_USERNAME,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_NAME,
    synchronize: true,
    // Tried using 'extra' as saw in another issue. Also tried: 'utf8', 'utf8_general_ci', 'utf8mb4'
    charset: "UTF8_GENERAL_CI", 
    entities: [
        Family
    ]
}

Here’s my entity:

@Entity()
export class Family {

    @PrimaryGeneratedColumn()
    id: number;

    @Column({name: 'sCatName', type: "varchar", length: 50})
    nameHebrew: string;

    @Column({name: 'sCatNameEng', type: "varchar", length: 50})
    nameEnglish: string;
}

Here’s the result of the query SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'arihav' AND TABLE_NAME = 'family';

TABLE_CATALOG	TABLE_SCHEMA	TABLE_NAME	COLUMN_NAME	ORDINAL_POSITION	COLUMN_DEFAULT	IS_NULLABLE	DATA_TYPE	CHARACTER_MAXIMUM_LENGTH	CHARACTER_OCTET_LENGTH	NUMERIC_PRECISION	NUMERIC_SCALE	DATETIME_PRECISION	CHARACTER_SET_NAME	COLLATION_NAME	COLUMN_TYPE	COLUMN_KEY	EXTRA	PRIVILEGES	COLUMN_COMMENT	GENERATION_EXPRESSION
def	arihav	family	id	1	NULL	NO	int	NULL	NULL	10	0	NULL	NULL	NULL	int(11)	PRI	auto_increment	select,insert,update,references		
def	arihav	family	sCatName	2	NULL	NO	varchar	50	50	NULL	NULL	NULL	latin1	latin1_swedish_ci	varchar(50)			select,insert,update,references		
def	arihav	family	sCatNameEng	3	NULL	NO	varchar	50	50	NULL	NULL	NULL	latin1	latin1_swedish_ci	varchar(50)			select,insert,update,references		

Note that the default is latin1 and not utf8

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
dmythrocommented, Oct 1, 2020

I’ve seen as a workaround only specifying to each column, not even per @Entity(), which is pretty annoying:

@Column({
  charset: 'utf8',
  collation: 'utf8_general_ci',
})
1reaction
andyslackcommented, Jan 20, 2022

I had an issue storing emoji’s in my table and this advice above worked for me:

@Column({ charset: 'utf8mb4', collation: 'utf8mb4_unicode_ci', })

On the column I wanted to store the emoji

Then updating the column via SQL:

ALTER TABLE order_logs MODIFY message CHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Fix the Collation and Character Set of a MySQL ...
How to Fix the Collation and Character Set of a MySQL Database manually ; mysql> ALTER ; SELECT CONCAT('ALTER TABLE `' ; SELECT...
Read more >
How to change the default charset of a MySQL table?
If you want to change the table default character set and all character columns to a new character set, use a statement like...
Read more >
How to Change Character Set from latin1 to UTF8 in MySQL
Similarly, here's the command to change character set of MySQL table from latin1 to UTF8. Replace table_name with your database table name.
Read more >
10.7 Column Character Set Conversion
To convert a binary or nonbinary string column to use a particular character set, use ALTER TABLE . For successful conversion to occur,...
Read more >
Cannot change collation on my MySQL database for all ...
ALTER TABLE `dbname`.`tablename` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;. Then, you just copy/ ...
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