Change default column name sequelize
I am working with Nodejs, Sequelize . This is the following structure for my user model:
"use strict";
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define('user', {
first_name: {
type: DataTypes.STRING,
allowNull: false,
},
last_name: {
type: DataTypes.STRING,
allowNull: false,
},
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
validate: {
isUnique: function (email, done) {
User.find({ where: { email: email }})
.done(function (err, user) {
if (err) {
done(err);
}
if (user) {
done(new Error('Email already registered'));
}
done();
});
}
}
},
profile_image_path: {
type: DataTypes.STRING,
allowNull: true,
},
active_flag: {
type: DataTypes.BOOLEAN,
allowNull: true,
defaultValue: true
},
delete_flag: {
type: DataTypes.BOOLEAN,
allowNull: true,
defaultValue: false
}
}, {
classMethods: {
associate: function(models) {
User.hasMany(models.account),
User.hasMany(models.user_has_contact)
}
}
});
return User;
};
By default, 3 columns are created:
- ID
- createdAt
- updatedAt
I do not want createdAt
and updatedAt
in this format, I want created_at
and updated_at
. How do I change this default column name ???
I have another table user_has_contact
with the following ratio:
{
classMethods: {
associate: function(models) {
UserHasContact.belongsTo(models.user)
}
}
}
Automatically creates a field userId
, but again I want it to be user_id
. Any suggestion would be appreciated!
source to share
sequelize.define('user', ..., {
createdAt: 'created_at',
updatedAt: 'updated_at',
});
http://docs.sequelizejs.com/en/latest/docs/models/#configuration
UserHasContact.belongsTo(models.user, { foreignKey: 'user_id' })
http://docs.sequelizejs.com/en/latest/docs/associations/#foreign-keys
source to share