月&盾

生活的精彩要去发现,而不是等待出现

文章详情

sequelize.js不能将驼峰camelCased命名转化为下划线underscored命名

定义Model的时候有这样两个参数: underscored,underscoredAll,

Converts all camelCased columns to underscored if true. Will not affect timestamp fields named explicitly by model options and will not affect fields with explicitly set field option

其意思是说转化所有驼峰字段为下划线字段,但实际情况并不是如此。

var Model = sequelize.define(
    'Assistance', {
        title: Sequelize.STRING,
        description: Sequelize.STRING,
        fullName: Sequelize.STRING
    }, {
        underscored: true,
        tableName: 'assistance',
        charset: 'utf8',
        collate: 'utf8_unicode_ci'
    }
);
CREATE TABLE IF NOT EXISTS `assistance` (`id` INTEGER NOT NULL auto_increment , `title` VARCHAR(255), `description` VARCHAR(255), `fullName` VARCHAR(255), `created_at` DATETIME NOT NULL, `updated_at` DATETIME NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_unicode_ci;

其中fullName字段在创建表的时候还是和模型中保持一致,并没有转换为full_name,underscored的设置其实只影响到了createdAt和updatedAt等内置字段。 在github上也有相关讨论:https://github.com/sequelize/sequelize/issues/6423

关键字词:sequelize.js