OmitNull option is ignored for Id's

232 views
Skip to first unread message

Chuck Conway

unread,
Nov 16, 2013, 11:35:54 PM11/16/13
to sequ...@googlegroups.com
First I want to thank all the contributors to Sequelize. It's an amazing ORM and most of all the documentation is in depth.

On to my question. 

I've created a simple table:

-- ----------------------------
-- Table structure for Organization
-- ----------------------------
DROP TABLE IF EXISTS
"public"."Organization";
CREATE TABLE
"public"."Organization" (
"Id" int4 primary key,
"Name" varchar(200) COLLATE "default" NOT NULL,
"Description" varchar(500) COLLATE "default",
"Url" varchar(250) COLLATE "default"
)
WITH
(OIDS=FALSE)


;




-- ----------------------------
-- Alter Sequences Owned By
-- ----------------------------


create sequence
Organization_id_seq;
alter table
"Organization" alter "Id" set default nextval('Organization_id_seq');

I am auto incrementing the key in the database. I have defined the table in code and then I am creating a new organization.

api.saveOrganization = function(req, res){


   
var sequelize = new Sequelize('ladder', 'postgres', 'password', {host:'192.168.0.112', port:5432, dialect: 'postgres', omitNull: true});
   
var Organization = sequelize.define('Organization',
       
{
           
Id:{type:Sequelize.INTEGER, primaryKey:true},
           
Name: Sequelize.STRING(200),
           
Description: Sequelize.STRING(500),
           
Url: Sequelize.STRING(250)
       
},
       
{
            freezeTableName
: true,
            timestamps
: false
       
});


   
Organization.create({Name:'Badminton Alley', Description:'A good place to buy badminton supplies', Url:'http://chuckconway.com'})
       
.success(function(){res.json({status:"Ok"})});


};

When I call the create method, I am getting an exception.

C:\www\ladder>node server.js
App started!
Executing: INSERT INTO "Organization" ("Id","Name","Description","Ur
l"
) VALUES (NULL,'Badminton Alley','A good place to buy badminton supplies','htt
p://chuckconway.com'
) RETURNING *;


events
.js:72
       
throw er; // Unhandled 'error' event
             
^
error
: null value in column "Id" violates not-null constraint
    at p
.parseE (C:\www\ladder\node_modules\sequelize-postgres\node_modules\pg\l
ib
\connection.js:473:11)
    at p
.parseMessage (C:\www\ladder\node_modules\sequelize-postgres\node_module
s
\pg\lib\connection.js:348:17)
    at
Socket.<anonymous> (C:\www\ladder\node_modules\sequelize-postgres\node_mo
dules
\pg\lib\connection.js:84:22)
    at
Socket.EventEmitter.emit (events.js:95:17)
    at
Socket.<anonymous> (_stream_readable.js:746:14)
    at
Socket.EventEmitter.emit (events.js:92:17)
    at emitReadable_
(_stream_readable.js:408:10)
    at emitReadable
(_stream_readable.js:404:5)
    at readableAddChunk
(_stream_readable.js:165:9)
    at
Socket.Readable.push (_stream_readable.js:127:10)

Even though I am have omitnull enable it is ignoring the Id and passing a null value to the database. The database then throws chunks.

I tracked the issue down to the Utils.removeNullValuesFromHash method (line 417). The offending code is key.match(/Id$/). Once I remove that evaluation it works fine.

  removeNullValuesFromHash: function(hash, omitNull, options) {
    var result = hash

    options = options || {}
    options.allowNull = options.allowNull || []

    if (omitNull) {
      var _hash = {}

      Utils._.each(hash, function(val, key) {
        if (options.allowNull.indexOf(key) > -1 || key.match(/Id$/) || ((val !== null) && (val !== undefined))) {
          _hash[key] = val
        }
      })

      result = _hash
    }

    return result
  },

My question is, what's the purpose of this evaluation? Why are the nulls ignored on Id's? 


Mick Hansen

unread,
Dec 31, 2014, 4:07:19 AM12/31/14
to sequ...@googlegroups.com
For anyone finding this later on. If the primary key attribute is not defined as `autoIncrement: true` it will assume you want to provide a value to it. 
`omitNull` is mostly deprecated now.
Reply all
Reply to author
Forward
0 new messages