AutoIncrement 在 NodeJs 的 Sequelize 中是如何工作的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15737949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:04:13  来源:igfitidea点击:

How does autoIncrement work in NodeJs's Sequelize?

node.jspostgresql-9.1auto-incrementsequelize.js

提问by Eric H.

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.

非常感谢。

采纳答案by Yacine Rezgui

normally Sequelize.js adapt itself with the databse. So the autoincrement attribute corresponds to a serial type in PostgreSQL.

通常 Sequelize.js 适应数据库。所以autoincrement 属性对应的是PostgreSQL 中的一个serial 类型。

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 是否不会为您添加它。

回答by mxgrn

omitNullconfig option by default is false. Set it to true, and the idwill be taken care of by Postgres:

omitNull配置选项默认为 false。将其设置为 true,id将由 Postgres 处理:

sequelize = new Sequelize('mydb', 'postgres', null, {
  host: 'localhost',
  port: 5432,
  dialect: 'postgres',
  omitNull: true
})

Visitor = sequelize.define('visitor', {
  email: Sequelize.STRING
})

Visitor.create({email: '[email protected]'})

回答by AdelaF

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)..."