Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node, Sequelize, Mysql - How to define collation and charset to models?

Im using sequelize /w node and node-mysql.

I create models using the sequelize-cli, and this is the result:

'use strict';
module.exports = function(sequelize, DataTypes) {
  let songs = sequelize.define('songs', {
    name: DataTypes.STRING,
    link: DataTypes.STRING,
    artist: DataTypes.STRING,
    lyrics: DataTypes.TEXT,
    writer: DataTypes.STRING,
    composer: DataTypes.STRING
  });

  return songs;
};

I want to be able to define collation and charset to each property of the model. the default collation is 'latin1_swedish_ci', and i need it in 'utf-8'.

Anyone? Tnx

like image 851
Eyal Cohen Avatar asked Jan 15 '17 22:01

Eyal Cohen


People also ask

How do you define a model in Sequelize?

Models can be defined in two equivalent ways in Sequelize: Calling sequelize. define(modelName, attributes, options) Extending Model and calling init(attributes, options)

How do I set default value in model Sequelize?

When you create a Sequelize model, you can add the default value for your model by adding the defaultValue option to the column(s) definition. The defaultValue option will be used by Sequelize to define default value(s) for your SQL column(s) when you create a table using Sequelize.

Does Sequelize have one relationship?

Using the Sequelize hasOne() association method. The Sequelize hasOne() association method is used to establish an association between two defined Sequelize models. The association method allows you to link two models so that you can retrieve data from both tables with one query execution.


1 Answers

In the part where u define sequelize

var sequelize = new Sequelize('database', 'username', 'password', {
  define: {
    charset: 'utf8',
    collate: 'utf8_general_ci', 
    timestamps: true
  },
  logging:false
});

For Table level Changing

sequelize.define('songs', {
  name: DataTypes.STRING,
  link: DataTypes.STRING,
  artist: DataTypes.STRING,
  lyrics: DataTypes.TEXT,
  writer: DataTypes.STRING,
  composer: DataTypes.STRING
}, {
  charset: 'utf8',
  collate: 'utf8_unicode_ci'
});
like image 131
Santhosh S Kashyap Avatar answered Oct 11 '22 23:10

Santhosh S Kashyap