我将 PostgreSQL 与 Sequelize ORM 结合使用,我希望在删除所有播种器并再次创建它们(sequelize db:seed:undo:all、sequelize db:seed:all)后,它们的自动增量将重置为 0。
找不到解决方案,提前感谢您的帮助。
我迟到了,但这就是我解决问题的方法。
这是表“Schede”的种子的 Down() 方法: 我添加到选项(第三个参数)
truncate
和cascade
。
文档:https://sequelize.org/master/class/lib/dialects/abstract/query-interface.js~QueryInterface.html
down: async (queryInterface, seq) => {
return queryInterface.bulkDelete('Schede', null, { truncate: true, cascade: true })
}
有两种方法可以将序列重置为 0。
ALTER SEQUENCE product_id_seq RESTART WITH 0;
。该命令会将序列的下一个值设置为 0,因此下一条语句将使用 1。TRUNCATE TABLE product RESTART IDENTITY;
。这将删除产品表中的所有行。这是续集代码:
const { QueryTypes } = require('sequelize');
// Reset auto increment for a specific table
await sequelize.query("ALTER SEQUENCE product_id_seq RESTART WITH 0", { type: QueryTypes.RAW });
// Truncate table and reset auto increment
await sequelize.query("TRUNCATE TABLE product RESTART IDENTITY", { type: QueryTypes.RAW });