Skip to content

Bug? db:create cannot set default charset with connection information. #590

Closed

Description

@ALiangLiang

What you are doing?

// config/config.js
require('dotenv').config()
module.exports = {
  development: {
    username:: process.env.DEV_DB_USER,
    password: process.env.DEV_DB_PASS,
    database: process.env.DEV_DB_NAME, //database_dev
    host: process.env.DEV_DB_HOST,
    dialect: process.env.DEV_DB_TYPE,
    dialectOptions: {
      charset: 'utf8mb4',
      collate: 'utf8mb4_general_ci'
    },
    logging: true
  }
}
$ sequelize db:create --env development

What do you expect to happen?

Create a database with charset in config/config.js

Executing (default): CREATE DATABASE `database_dev` DEFAULT CHARSET=`utf8mb4 DEFAULT COLLATE=`utf8mb4_general_ci`

What is actually happening?

Doesn't use this configure.

# log
Executing (default): CREATE DATABASE `database_dev`

Dialect: mysql
Database version: 5.7.20
Sequelize CLI version: 3.1.0
Sequelize version: 4.22.15

Activity

  1. chris-fung commented on Jan 10, 2018

    @chris-fung

    Met the same problem with yours. Just need to modify the charset and collate by myself after creating db

  2. galki commented on Feb 23, 2018

    @galki

    according to this using define instead of dialectOptions works when creating a sequelize connection, but it doesn't work when using cli to db:create

    i also tried to set the charset/collation in the model migration like the docs say by adding the third arg to:

    queryInterface.createTable(
      tableName,
      { /*... columns*/ },
      { charset: 'utf8' }
    )
    

    and that results in error:

    ERROR: Can't create table `mydb`.`mytable` (errno: 150 "Foreign key constraint is incorrectly formed")
    

    Sequelize CLI version: 4.0.0

  3. kwbtdisk commented on Mar 2, 2018

    @kwbtdisk

    I avoid this problem by querying ALTER DATABASE from a migration file like this:

    /db/migrations/00000-fix-db-charset.js

    module.exports = {
      up: (queryInterface, Sequelize) => {
        return queryInterface.sequelize.query(
            `ALTER DATABASE ${queryInterface.sequelize.config.database}
            CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;`
          )
      },
      down: (queryInterface, Sequelize) => { }
    }

    With naming the file like 00000-xxxxx.js, we can throw the query just before running other migrations.

  4. jeanpchr commented on Apr 19, 2018

    @jeanpchr

    +1

  5. perzanko commented on Sep 3, 2018

    @perzanko

    Problem occured for me as well.

  6. ALiangLiang commented on Nov 4, 2018

    @ALiangLiang
    ContributorAuthor

    @Huangyanyang
    No, but you can specify with args. sequelize db:create --charset <CHARSET>

  7. vishwasraj commented on Dec 23, 2018

    @vishwasraj

    @Huangyanyang
    No, but you can specify with args. sequelize db:create --charset <CHARSET>

    It would have been good. If it was taken from the config file.

  8. rcupic commented on Apr 2, 2019

    @rcupic

    I avoid this problem by querying ALTER DATABASE from a migration file like this:

    /db/migrations/00000-fix-db-charset.js

    module.exports = {
      up: (queryInterface, Sequelize) => {
        return queryInterface.sequelize.query(
            `ALTER DATABASE ${queryInterface.sequelize.config.database}
            CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;`
          )
      },
      down: (queryInterface, Sequelize) => { }
    }

    With naming the file like 00000-xxxxx.js, we can throw the query just before running other migrations.

    This solution worked for me. Is there any update on this by sequelize team?

  9. abhijeet029 commented on Apr 29, 2020

    @abhijeet029

    add { charset: 'utf8' } in your migration file after the data column object

  10. rem711 commented on Apr 28, 2021

    @rem711

    I avoid this problem by querying ALTER DATABASE from a migration file like this:

    /db/migrations/00000-fix-db-charset.js

    module.exports = {
      up: (queryInterface, Sequelize) => {
        return queryInterface.sequelize.query(
            `ALTER DATABASE ${queryInterface.sequelize.config.database}
            CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;`
          )
      },
      down: (queryInterface, Sequelize) => { }
    }

    With naming the file like 00000-xxxxx.js, we can throw the query just before running other migrations.

    It works for me like that, but if someone is still experiencing the same issue, I needed to use back quotes around database name `${queryInterface.sequelize.config.database}` or it throws a syntax error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions