Hi,i'm trying to get custom validation on one column of my model:In my model definition:var Device = sequelize.define('Device', { model_name: DataTypes.STRING, imei: DataTypes.STRING, serial_number: { type: DataTypes.STRING, validate: { customValidation(value) { Device.findOne({ where: {serial_number: value} }) .then(function() { throw new Error('serial number already exists!') }) } } } Record creation:Device.bulkCreate(csvOutput, { validate: true }) .then(function() { console.log('Records inserted into database') }) .catch(function(err) { console.log('Error encountered: ' + err) }) When I try to insert duplicate entries, the validation seemed to pick it up - I could see the 'serial number already exists!' message on the terminal, but the records ended up being inserted into the db anyway.Can someone point out where i've gone wrong?
Submitted June 01, 2017 at 05:26PM by raccoonranger73
No comments:
Post a Comment