postgresql - postgres/sequelize 中的自动增量

标签 postgresql sequelize.js

我有一个使用 Sequelize (node/express) 作为 ORM 的 Postgres 数据库。我有一个名为学生的表,其中有列:id 和 name。
在这个student表中,我有几个注册的学生,但是有一个细节:最后一个注册的ID是34550,第一个是30000,他们来自以前数据库的导入,我需要从34550继续计数,也就是从最后注册的学生。但是,当我通过 API 注册学生时,生成的 ID 低于 30000。我知道在 mysql 中,ID 字段为 AUTO INCREMENT 可以解决它,但是,据我所知,postgres 的工作方式不同。
我怎么能解决这个问题?
用于创建表的迁移如下:

    module.exports = {
    up: (queryInterface, Sequelize) => {
      return queryInterface.createTable('students', {
        id: {
          type: Sequelize.INTEGER,
          allowNull: false,
          autoIncrement: true,
          primaryKey: true,
        },
        name: {
          type: Sequelize.STRING,
          allowNull: false,
        },
      });
    },
  
    down: (queryInterface) => {
      return queryInterface.dropTable('students');
    },
  };
表格打印:
enter image description here

最佳答案

基于 Frank 评论,我能够使用以下方法进行调整:

SELECT setval('public.students_id_seq', 34550, true);

关于postgresql - postgres/sequelize 中的自动增量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67887091/

相关文章:

postgresql - PostgreSQL 中的时间聚合

database - 备份和恢复选项在 pgAdmin III 中不可用

php - 如何在 php 中准备对 json postgresql 字段的查询

mysql - 在 HasOne 关系中获取关联错误

mysql - Sequelize - 匹配 mySQL 数据库中给定实体的所有记录

orm - Sequelize - where 子句中的子查询

javascript - 多对多关联 sequelize.js Node 中未显示关联模型

postgresql - 基于第二张表数据的部分索引

Postgresql - 如何安全地重命名表

javascript - 为什么我的退出进程在循环结束之前执行?