gpt4 book ai didi

node.js - sequelize index 选项运行两次并导致测试失败

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

这是我的模型:


var PaymentsToMonths = db.define('PaymentsToMonths', {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
PaymentPlanDetailID: {
type: Sequelize.DECIMAL,
allowNull: false,
},
month: {
type: Sequelize.DECIMAL,
allowNull: false,
// todo add validation 1-12
},
year: {
type: Sequelize.DECIMAL,
allowNull: false,
},
sum: {
type: Sequelize.DECIMAL,
allowNull: false,
},
monthTotal: {
type: Sequelize.DECIMAL,
allowNull: false,
}

}, {
tableName: 'PaymentsToMonths',
freezeTableName: true, // Model tableName will be the same as the model name
timestamps: true,
indexes: [
{
unique: true,
fields: ['PaymentPlanDetailID', 'month', 'year'],
name: 'unique_payment_per_month_and_year'
}
]
})
运行测试时,我看到此错误:
  console.error
SequelizeDatabaseError: Duplicate key name 'unique_payment_per_month_and_year'
这是由于运行这两个相同的行:
  console.log
Executing (default): ALTER TABLE `PaymentsToMonths` ADD UNIQUE INDEX `unique_payment_per_month_and_year` (`PaymentPlanDetailID`, `month`, `year`)

at Sequelize.log (../node_modules/sequelize/lib/sequelize.js:1171:15)

console.log
Executing (default): ALTER TABLE `PaymentsToMonths` ADD UNIQUE INDEX `unique_payment_per_month_and_year` (`PaymentPlanDetailID`, `month`, `year`)
为什么会这样?
不确定是否相关,但我也有此代码用于测试:
afterAll(async (done) => {
jest.restoreAllMocks();

// remove all data and tables
await sequelize.drop();

// to end the jest process
done();

})

最佳答案

原来这是由于使用

sequelize.sync({force: false})
在我的 main_test.js 文件中。
愚蠢的错误..
删除其中一个工作。

关于node.js - sequelize index 选项运行两次并导致测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63135738/

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