Cannot change charset of MySQL database tables
See original GitHub issueIssue 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:
- Created 5 years ago
- Reactions:5
- Comments:8 (3 by maintainers)
Top GitHub Comments
I’ve seen as a workaround only specifying to each column, not even per
@Entity()
, which is pretty annoying: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;