How to set collation for a specific Column in a Sequelize model?
Asked Answered
A

2

10

How to set collation for a specific Column in a Sequelize model?

I tried this:

name: {
  type: Sequelize.STRING,
  allowNull: false,
  collate: 'utf8_general_ci'
},

Apparently it doesn't work. Any other ideia?

Achlamydeous answered 10/8, 2018 at 15:34 Comment(8)
do you want to map the column named "name", in this case?Ku
yes, or for any other.Sergent
sorry @Tiago Bértolo I thought your problem was in the construction of the model. Had not read the word "collate"Ku
No problem mate. Thanks for trying.Sergent
Try this Sequelize.STRING + ' CHARSET utf8 COLLATE utf8_general_ci' in your typeKu
Didn't work! Nice attempt though! :DSergent
hum... this works for meKu
What your sequelize version? Your different charset is only for this column or all database?Ku
A
16

There are several things you can do to support:

Table level:

sequlize.define('table', {

}, {
    charset: 'utf8',
    collate: 'utf8_general_ci'
})

Column level:

sequelize.define('table', {
    column: Sequelize.STRING + ' CHARSET utf8 COLLATE utf8_general_ci'
})

Database level:

let sequelize = new Sequelize('database', {
    dialectOptions: {
        charset: 'utf8',
        collate: 'utf8_general_ci',
    }
});
Alfeus answered 10/8, 2018 at 16:24 Comment(1)
Ok... Let's try, at some point, we can get it right. Set the define in sequelize... whith the column level sample. Outside your modelKu
A
0

What It worked for me

await queryInterface.sequelize.query(`
    ALTER TABLE files
    MODIFY COLUMN original_name VARCHAR(255)
    CHARACTER SET utf8mb4
    COLLATE utf8mb4_general_ci NOT NULL;
`);
Ariannaarianne answered 15/8, 2024 at 22:26 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.