Unhandled rejection SequelizeDatabaseError: null value in column "firstName" violates not-null constraint
var bcrypt = require('bcrypt')
, db = require('../db').sequelize
, Account = db.model('Account')
, User = db.model('User')
;
module.exports = function(req, res, next){
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash(req.body.password, salt, function(err, hash) {
var account = Account.build({
name:req.body.accountName
});
account.save()
.then(function(){
var user = User.build({
firstName: req.body.firstName,
lastName: req.body.lastName,
email: req.body.email,
password: hash
})
user.setAccount(account);
return user.save()
.then(function(){
res.send({
id: user.id,
accountId: account.id
})
})
.catch(function(err){
next(err);
});
})
.catch(function(err){
next(err);
})
});
});
}
'use strict'; module.exports = { up: function (queryInterface, Sequelize) { return queryInterface.createTable('Users', { id: { allowNull: false, autoIncrement: true, primaryKey: true, type: Sequelize.INTEGER }, accountId: { type: Sequelize.INTEGER, references: "Accounts", referencesKey: "id", allowNull: false }, firstName: { type: Sequelize.STRING, allowNull: false }, lastName: { type: Sequelize.STRING, allowNull: false }, email: { type: Sequelize.STRING, allowNull: false, unique: true }, password: { type: Sequelize.STRING, allowNull: false }, createdAt: { allowNull: false, type: Sequelize.DATE }, updatedAt: { allowNull: false, type: Sequelize.DATE } }); }, down: function (queryInterface, Sequelize) { return queryInterface.dropTable('Users'); } };
And here's my model:
'use strict'; var bcrypt = require('bcrypt'); module.exports = function(sequelize, DataTypes) { var User = sequelize.define('User', { accountId: { type: DataTypes.INTEGER, references: "Accounts", referencesKey: "id", allowNull: false }, firstName: { type:DataTypes.STRING, allowNull:false }, lastName: { type:DataTypes.STRING, allowNull:false }, email: { type:DataTypes.STRING, allowNull:false, unique: true }, password: { type:DataTypes.STRING, allowNull:false } }, { classMethods: { associate: function(models) { User.belongsTo(models.Account, { foreignKey: 'accountId' }) } }, instanceMethods: { verifyPassword: function(password, done) { return bcrypt.compare(password, this.password, function(err, res) { return done(err, res); }); } } }); return User; };And firstName definitely isn't null in the code or after the write succeeds against the database. FYI I'm using Postgres 9.4.1 RDS.
listening at http://:::8288
Executing (default): INSERT INTO "Accounts" ("id","name","updatedAt","createdAt") VALUES (DEFAULT,'Test','2015-09-12 18:30:43.297 +00:00','2015-09-12 18:30:43.297 +00:00') RETURNING *;
Executing (default): INSERT INTO "Users" ("accountId","updatedAt","createdAt") VALUES (7,'2015-09-12 18:30:43.404 +00:00','2015-09-12 18:30:43.404 +00:00');
Executing (default): INSERT INTO "Users" ("id","accountId","firstName","lastName","email","password","createdAt","updatedAt") VALUES (DEFAULT,7,'first','last','em...@email.com','hashedPassword','2015-09-12 18:30:43.404 +00:00','2015-09-12 18:30:43.409 +00:00') RETURNING *;
Unhandled rejection SequelizeDatabaseError: null value in column "firstName" violates not-null constraint
var bcrypt = require('bcrypt')
, db = require('../db').sequelize
, Account = db.model('Account')
, User = db.model('User')
, auth = require('../auth')
;
module.exports = function(req, res, next){
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash(req.body.password, salt, function(err, hash) {
Account.create({
name:req.body.accountName
})
.then(function(account){
return User.create({
firstName: req.body.firstName,
lastName: req.body.lastName,
email: req.body.email,
password: hash,
accountId:account.id
})
})
.then(function(user){
return res.send({token:auth.createToken(user)});Ok, but at the moment setAccount is called all the necessary information exists on the object. Why didn't it issue the insert with all o f my user fields?