[英]Node, Sequelize, Mysql - How to define collation and charset to models?
Im using sequelize /w node and node-mysql.我使用 sequelize /w node 和 node-mysql。
I create models using the sequelize-cli, and this is the result:我使用 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; };
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'.默认排序规则是“latin1_swedish_ci”,我需要它在“utf-8”中。
Anyone?任何人? Tnx Tnx
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'
});
is very simple to add utf-8 just go to any model that you have and do this for example (i edit your code):添加 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.