繁体   English   中英

Node、Sequelize、Mysql - 如何为模型定义排序规则和字符集?

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

我使用 sequelize /w node 和 node-mysql。

我使用 sequelize-cli 创建模型,结果如下:

 '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; };

我希望能够为模型的每个属性定义排序规则和字符集。 默认排序规则是“latin1_swedish_ci”,我需要它在“utf-8”中。

任何人? Tnx

在你定义续集的部分

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

对于表级别更改

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'
});

添加 utf-8 非常简单,只需转到您拥有的任何模型并执行此操作,例如(我编辑您的代码):

module.exports = function(sequelize, DataTypes) {
  let songs = sequelize.define('songs', {
    name: {DataTypes.STRING,allowNull : false},
    link: {DataTypes.STRING,allowNull : false},
    artist: {DataTypes.STRING,allowNull : false},
    lyrics: {DataTypes.TEXT,allowNull : false},
    writer: {DataTypes.STRING,allowNull : false},
    composer: {DataTypes.STRING,allowNull : false}
  }, {
charset: 'utf8', /* i add this two ligne here for generate the table with collation  = 'utf8_general_ci' test it and tell me ? */
collate: 'utf8_general_ci'


});

  return songs;
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM