gpt4 book ai didi

postgresql - postgres/sequelize 中的自动增量

转载 作者:行者123 更新时间:2023-12-03 22:30:00 24 4
gpt4 key购买 nike

我有一个使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com