{"id":7867,"date":"2015-11-02T01:48:07","date_gmt":"2015-11-02T01:48:07","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/02\/how-to-prevent-sequelize-from-inserting-null-for-primary-keys-with-postgres-open-source-projects-sequelize-sequelize\/"},"modified":"2015-11-02T01:48:07","modified_gmt":"2015-11-02T01:48:07","slug":"how-to-prevent-sequelize-from-inserting-null-for-primary-keys-with-postgres-open-source-projects-sequelize-sequelize","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/02\/how-to-prevent-sequelize-from-inserting-null-for-primary-keys-with-postgres-open-source-projects-sequelize-sequelize\/","title":{"rendered":"How to prevent Sequelize from inserting NULL for primary keys with Postgres-open source projects sequelize\/sequelize"},"content":{"rendered":"<p>I have created a table in postgresql 9<\/p>\n<pre><code>create table stillbirth(id serial primary key, state varchar(100), count int not null, year int not null); \n<\/code><\/pre>\n<p>trying to write a sample on node.js with sequelize 1.4.1 version.<\/p>\n<p>mapped the above table as<\/p>\n<pre><code>var StillBirth = sequelize.define('stillbirth',\n{ id: {type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true},\nstate: Sequelize.STRING,\nyear: Sequelize.INTEGER,\ncount: Sequelize.INTEGER\n}, {timestamps: false, freezeTableName: true});\n<\/code><\/pre>\n<p>now when i try to create a new instance of Stillbirth and save it, i get errors.<\/p>\n<p>\/** new instance create code **\/<\/p>\n<pre><code>StillBirth\n   .build({state: objs[j].state, year: objs[j].year, count: objs[j].count})\n   .save()\n   .error(function(row){\n         console.log('could not save the row ' + JSON.stringify(row));\n        })\n   .success(function(row){\n       console.log('successfully saved ' + JSON.stringify(row));\n   })\n<\/code><\/pre>\n<p>error i get<\/p>\n<p>*Executing: INSERT INTO &#8220;stillbirth&#8221; (&#8220;state&#8221;,&#8221;year&#8221;,&#8221;count&#8221;,&#8221;id&#8221;) VALUES (&#8216;Andhra Pradesh&#8217;,2004,11,NULL) RETURNING <em>; could not save the row {&#8220;length&#8221;:110,&#8221;name&#8221;:&#8221;error&#8221;,&#8221;severity&#8221;:&#8221;ERROR&#8221;,&#8221;code&#8221;:&#8221;23502&#8243;,&#8221;file&#8221;:&#8221;execMain.c&#8221;,&#8221;line&#8221;:&#8221;1359&#8243;,&#8221;routine&#8221;:&#8221;ExecConstraints&#8221;}<\/em><\/p>\n<p>If you look at the sql that its generating, it puts null for the primary key which should ideally be generated by the db.<\/p>\n<p>Can someone help me as to what am i missing here ?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have created a table in postgresql 9 create table stillbirth(id serial primary key, state varchar(100), count int not null, year int not null); trying to write a sample on node.js with sequelize 1.4.1 version. mapped the above table as var StillBirth = sequelize.define(&#8216;stillbirth&#8217;, { id: {type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true}, state: Sequelize.STRING, year: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7867","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7867","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7867"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7867\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}