mysqlnode.jssequelize.jssequelize-cli

Is it possible to set charset with sequelize migrations?


I am working on a project where i had to set charset of database to

utf8mb4

but there is a problem even though i have defined charset in config file, when i run migrations using sequelize-cli it creates database and tables with

utf8

here is my config.json file

{
  "development": {
    "username": "root",
    "password": "root",
    "database": "coinscout",
    "host": "127.0.0.1",
    "dialect": "mysql",
    "define": {
      "charset": "utf8mb4",
      "collate": "utf8mb4_general_ci"
    }
  }
}

this creates a database with tables in utf8 charset but if i drop the database and run db.sequelize.sync(); from code it correctly creates table with given charset.

So i wanted to know what will be best way to change charset if i am using sequelize-cli to create and run migrations?


Solution

  • From the CLI

    sequelize db:create --charset utf8mb4 --collate utf8mb4_unicode_ci

    It can be a bit confusing because it is sometimes ambiguous when working with config and model files at what level the charset option applies to.