SequelizeDatabaseError: could not serialize access due to concurrent update
SequelizeDatabaseError: could not serialize access due to concurrent update
在 Mocha
测试 beforeEach
钩子中,我正在尝试 destroy
所有 table 记录。
import { db } from '../src/db/models';
export const truncateTable = () => {
const promises = Object.keys(db).map(key => {
if (key !== 'Sequelize' && key !== 'sequelize') {
console.log(key);
return db[key].destroy({ where: {} });
}
});
return Promise.all(promises);
};
然后在测试中,我是这样做的:
describe.only('application mutations', () => {
beforeEach(() => truncateTable());
...
我遇到的错误:
SequelizeDatabaseError: could not serialize access due to concurrent
update
TL/DR:在您的测试中,如果您想快速删除模型并重置您的数据库,请使用 sync
。
describe.only('application mutations', () => {
beforeEach(async () => {
await db.sync({force: true})
});
}
如果你想单独销毁你的模型,你必须在启动新模型之前适当地等待你的承诺完成。目前,您的承诺正在同时启动,因此出现 Sequelize 错误。
export const truncateTable = async () => {
const promises = Object.keys(db).map(key => {
if (key !== 'Sequelize' && key !== 'sequelize') {
await db[key].destroy({ where: {} });
}
});
};
// in your test file
describe.only('application mutations', () => {
beforeEach(async () => {
await truncateTable();
});
})
在 Mocha
测试 beforeEach
钩子中,我正在尝试 destroy
所有 table 记录。
import { db } from '../src/db/models';
export const truncateTable = () => {
const promises = Object.keys(db).map(key => {
if (key !== 'Sequelize' && key !== 'sequelize') {
console.log(key);
return db[key].destroy({ where: {} });
}
});
return Promise.all(promises);
};
然后在测试中,我是这样做的:
describe.only('application mutations', () => {
beforeEach(() => truncateTable());
...
我遇到的错误:
SequelizeDatabaseError: could not serialize access due to concurrent update
TL/DR:在您的测试中,如果您想快速删除模型并重置您的数据库,请使用 sync
。
describe.only('application mutations', () => {
beforeEach(async () => {
await db.sync({force: true})
});
}
如果你想单独销毁你的模型,你必须在启动新模型之前适当地等待你的承诺完成。目前,您的承诺正在同时启动,因此出现 Sequelize 错误。
export const truncateTable = async () => {
const promises = Object.keys(db).map(key => {
if (key !== 'Sequelize' && key !== 'sequelize') {
await db[key].destroy({ where: {} });
}
});
};
// in your test file
describe.only('application mutations', () => {
beforeEach(async () => {
await truncateTable();
});
})