gpt4 book ai didi

postgresql - SequelizeJS 中的自定义 Postgresql 序列 Nextval

转载 作者:行者123 更新时间:2023-11-29 11:47:01 25 4
gpt4 key购买 nike

我如何在 sequelizeJS 模型中定义序列 nextval()

sequelizeJS 中的 nextval() 是否有任何预定义函数可用?,

是否有可能在 sequelize 定义模型中编写序列 nextval() 自定义函数?

最佳答案

如果此时有人也想这样做,我想最好的方法是使用 Sequelize.literal:

// on your model

const model = sequelize.define("model", {
...attributes,
customSequence: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: sequelize.Sequelize.literal("nextval('custom_sequence')")
},
})

// after create a migration for your new column

module.exports = {
up: async (queryInterface, sequelize) => {
await queryInterface.sequelize.query("CREATE SEQUENCE custom_sequence start 1020 increment 20",)

await queryInterface.addColumn('table', 'customSequence', {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: sequelize.Sequelize.literal("nextval('custom_sequence')")
})
},

down: async (queryInterface, sequelize) => {
await queryInterface.sequelize.query('DROP SEQUENCE custom_sequence')
await queryInterface.removeColumn('table', 'customSequence')
}
};

关于postgresql - SequelizeJS 中的自定义 Postgresql 序列 Nextval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42501904/

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