问题
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