Is there no option to map the column name in sequelize model

心已入冬 提交于 2020-05-26 10:27:08

问题


I have a given database with long, cumbersome columnnames. Isn't there any way to map the tablenames to shorter and more descriptive propertyNames in the model ? something like

var Employee = sql.define('Employee', {
    id : {type : Sequelize.INTEGER , primaryKey: true, map : "veryLongNameForJustTheId"}
},{
     tableName: 'cumbersomeTableName',
     timestamps: false
});;

回答1:


id : {
    field: 'some_long_name_that_is_terrible_thanks_dba_guy',
    type : Sequelize.INTEGER ,
    primaryKey: true
}

Specify a 'field' attribute ... Like that ^




回答2:


You can specify a table name by supplying the name as the first parameter to the define() call. For example:

var User = sequelize.define(
'a_long_cumbersone_users_table_name',
{
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true
    },
    name: {
        type: Sequelize.STRING
    },
    email: {
        type: Sequelize.STRING
    },
    password: {
        type: Sequelize.STRING
    },
    rememberToken: {
        type: Sequelize.STRING,
        field: 'remember_token'
    }
},
{
    underscored: true,
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at'
}
);



回答3:


So far the best solution I found to do this is to use getters and setters.

They will - however - not affect results in object.values or object.toJSON(). You'll need your own serialization methods if you want to use these.



来源:https://stackoverflow.com/questions/17963516/is-there-no-option-to-map-the-column-name-in-sequelize-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!