• 分类:【Nodejs
  • 浏览【4446】
  • 评论【0】
  • 更新【2017-10-21 10:34:33】

定义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上也有相关讨论:

<Unhandled rejection CastError: Cast to ObjectId failed for value "catalog" at path "_id" for model "Blog"