我在 postgresql 9 中创建了一个表
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 "stillbirth" ("state","year","count","id") VALUES ('Andhra Pradesh',2004,11,NULL) RETURNING ; 无法保存行 {"length":110,"name":"error","severity":"ERROR","code":"23502","file":"execMain.c","line": "1359","例程":"ExecConstraints"}
如果您查看它生成的 sql,它会将 null 作为主键,理想情况下应该由 db 生成。
有人可以帮助我了解我在这里缺少什么吗?