What I'm trying to do here is automatically set a column value when the entry gets created.
So let's say that if I were to have a Posts
table which has the columns: :id :integer :primary :title :string :post :text :slug :string :unique
and I create a new entry of this with sequelize by calling Model.Posts.create
then I want the slug column to be automatically set to a value (the title slugified) when it gets created without having to do anything.
How would I have this be done?
You could use a beforeCreate hook:
sequelize.define('post', attributes, {
hooks: {
beforeCreate: function (instance) {
instance.set('slug', slugify(instance.get('title'));
}
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.