我在PostgreSQL的9如何防止Sequelize從插入主鍵NULL與Postgres的
create table stillbirth(id serial primary key, state varchar(100), count int not null, year int not null);
創建表時試圖寫上sequelize 1.4.1版本Node.js的一個樣本。
映射上表爲
var StillBirth = sequelize.define('stillbirth',
{ id: {type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true},
state: Sequelize.STRING,
year: Sequelize.INTEGER,
count: Sequelize.INTEGER
}, {timestamps: false, freezeTableName: true});
現在,當我嘗試創建死胎的新實例,並保存它,我得到的錯誤。
/**新實例創建代碼**/
StillBirth
.build({state: objs[j].state, year: objs[j].year, count: objs[j].count})
.save()
.error(function(row){
console.log('could not save the row ' + JSON.stringify(row));
})
.success(function(row){
console.log('successfully saved ' + JSON.stringify(row));
})
錯誤,我得到
*執行:INSERT INTO 「死胎」( 「國家」, 「年」, 「伯爵」,」 id「)VALUES('Andhra Pradesh',2004,11,NULL)返回; 無法保存行{「length」:110,「name」:「error」,「severity」:「ERROR」,「code」:「23502」,「file」:「execMain.c」,「line」 :「1359」,「routine」:「ExecConstraints」}
如果你看看它生成的sql,它會爲主鍵理想地由db產生null。
有人可以幫助我,我在這裏失蹤了什麼?
感謝那些與1.5.X版本的工作,令人驚訝的是NPM註冊表https://npmjs.org/package/sequelize犯規提及關於1.5.0版本的任何東西。 – anishek
在命令行上嘗試'npm show sequelize'。 :) – sdepold
此解決方案的問題在於,當您嘗試執行像這樣的user.save({address:null})更新時;它不會更改數據庫中的地址。有沒有辦法執行一個更新set屬性爲null? – lao