Sequelize's document doesn't say a whole lot about autoIncrement
. It only includes the following example:
Sequelize的文档并未详细说明autoIncrement。它只包括以下示例:
// autoIncrement can be used to create auto_incrementing integer columns
incrementMe: { type: Sequelize.INTEGER, autoIncrement: true }
Based off of this example I have the following code:
基于此示例,我有以下代码:
db.define('Entries', {
id: {
type: Seq.INTEGER,
autoIncrement: true,
primaryKey: true
},
title: {
type: Seq.STRING,
allowNull: false
},
entry: {
type: Seq.TEXT,
allowNull: false
}
}
Now when I want to create an entry I want to do something like this:
现在,当我想创建一个条目时,我想做这样的事情:
models.Entry.create({title:'first entry', entry:'yada yada yada'})
However, when I execute that code I get a database error:
但是,当我执行该代码时,我收到数据库错误:
Null value in column "id" violates not null constraint
列“id”中的空值违反非空约束
I would assume Sequelize would put the integer in for me or define the database table to do that itself. Apparently not? What am I missing here to ensure that the id is automatically incremented and filled?
我会假设Sequelize会为我提供整数或定义数据库表来自己做。显然不是?我在这里缺少什么来确保id自动递增和填充?
Thanks much.
非常感谢。
3 个解决方案
#1
15
normally Sequelize.js adapt itself with the databse. So the autoincrement attribute corresponds to a serial type in PostgreSQL.
通常Sequelize.js适应数据库。因此,autoincrement属性对应于PostgreSQL中的串行类型。
I already worked with Sequelize and what I remember is that you don't need to specify an id as a primary key. Sequelize will do it for you.
我已经使用过Sequelize,我记得你不需要指定id作为主键。 Sequelize会为你做的。
Try to don't add an id attribute and see if Sequelize will not add it for you or not.
尝试不添加id属性,看看Sequelize是否不会为你添加它。
#2
9
omitNull
config option by default is false. Set it to true, and the id
will be taken care of by Postgres:
默认情况下,omitNull配置选项为false。将其设置为true,并由Postgres处理id:
sequelize = new Sequelize('mydb', 'postgres', null, {
host: 'localhost',
port: 5432,
dialect: 'postgres',
omitNull: true
})
Visitor = sequelize.define('visitor', {
email: Sequelize.STRING
})
Visitor.create({email: 'foo@bar.com'})
#3
0
It worked for me if I added manualy the "id" field (in this case Sequelize will not add it automaticaly) in the model like this:
如果我在模型中添加了“id”字段(在这种情况下Sequelize不会自动添加它),它对我有用,如下所示:
Visitor = sequelize.define('visitor', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
email: Sequelize.STRING
})
result SQL command is: "CREATE TABLE IF NOT EXISTS "visitor" ("id" SERIAL, "email" varchar(255)..."
结果SQL命令是:“CREATE TABLE IF NOT EXISTS”visitor“(”id“SERIAL,”email“varchar(255)......”
#1
15
normally Sequelize.js adapt itself with the databse. So the autoincrement attribute corresponds to a serial type in PostgreSQL.
通常Sequelize.js适应数据库。因此,autoincrement属性对应于PostgreSQL中的串行类型。
I already worked with Sequelize and what I remember is that you don't need to specify an id as a primary key. Sequelize will do it for you.
我已经使用过Sequelize,我记得你不需要指定id作为主键。 Sequelize会为你做的。
Try to don't add an id attribute and see if Sequelize will not add it for you or not.
尝试不添加id属性,看看Sequelize是否不会为你添加它。
#2
9
omitNull
config option by default is false. Set it to true, and the id
will be taken care of by Postgres:
默认情况下,omitNull配置选项为false。将其设置为true,并由Postgres处理id:
sequelize = new Sequelize('mydb', 'postgres', null, {
host: 'localhost',
port: 5432,
dialect: 'postgres',
omitNull: true
})
Visitor = sequelize.define('visitor', {
email: Sequelize.STRING
})
Visitor.create({email: 'foo@bar.com'})
#3
0
It worked for me if I added manualy the "id" field (in this case Sequelize will not add it automaticaly) in the model like this:
如果我在模型中添加了“id”字段(在这种情况下Sequelize不会自动添加它),它对我有用,如下所示:
Visitor = sequelize.define('visitor', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
email: Sequelize.STRING
})
result SQL command is: "CREATE TABLE IF NOT EXISTS "visitor" ("id" SERIAL, "email" varchar(255)..."
结果SQL命令是:“CREATE TABLE IF NOT EXISTS”visitor“(”id“SERIAL,”email“varchar(255)......”