samedi 18 avril 2015

How to create a TRIGGER in SEQUELIZE (nodeJS)?

Im trying to create a trigger using sequelize.. the main idea is to create an instance of CONFIG after create a USER.



//USER MODEL
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define('User', {
name : DataTypes.STRING(255),
email : DataTypes.STRING(255),
username : DataTypes.STRING(45),
password : DataTypes.STRING(100),
}, {
classMethods : {
associate : function(models) {
User.hasOne(models.Config)
}
}
});
return User;
};

//CONFIG MODEL
module.exports = function(sequelize, DataTypes) {
var Config = sequelize.define('Config', {
notifications : DataTypes.INTEGER
}, {
classMethods : {
associate : function(models) {
Config.belongsTo(models.User)
}
}
});

return Config;
};


As you can see, a "user" has one "config" and a "config" belongs to a "user", so after a user is created I want to create his config row automatically.


The goal is to do:



DELIMITER //
CREATE TRIGGER create_config AFTER INSERT ON user
FOR EACH ROW
BEGIN
insert into config (user_id) values(new.user_id);
END; //
DELIMITER ;


How I'd do it?


Thank you!


Aucun commentaire:

Enregistrer un commentaire