作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在文档中它说:
try {
const result = await sequelize.transaction(async (t) => {
const user = await User.create({
firstName: 'Abraham',
lastName: 'Lincoln'
}, { transaction: t });
await user.setShooter({
firstName: 'John',
lastName: 'Boothe'
}, { transaction: t });
return user;
});
// If the execution reaches this line, the transaction has been committed successfully
// `result` is whatever was returned from the transaction callback (the `user`, in this case)
} catch (error) {
// If the execution reaches this line, an error occurred.
// The transaction has already been rolled back automatically by Sequelize!
}
export const executor = async (query: Function, db: any) => {
try {
const result = await db.sequelize.transaction(async t => {
const resp = await query(t);
return resp;
});
// If the execution reaches this line, the transaction has been committed successfully
// `result` is whatever was returned from the transaction callback (the `user`, in this case)
return result;
} catch (error) {
// If the execution reaches this line, an error occurred.
// The transaction has already been rolled back automatically by Sequelize!
// log here
throw new Error('Internal server error');
}
};
const r = executor(param => {
db.sequelize.query(
'CALL registerSportUser (:email, :password, :roleId, :firstName, :lastName, :age, :jobTitle, :prefLanguaged, :assigned, :sportId, :tableId, :position, :image, :imageName)',
{
replacements: {
email: args.input.Email,
password: PasswordHash,
roleId: args.input.RoleId,
firstName: args.input.FirstName,
lastName: args.input.LastName,
age: new Date(new Date(args.input.Age).toUTCString()),
jobTitle: args.input.JobTitle,
prefLanguaged: args.input.PrefLanguaged,
assigned: false,
sportId: '11ea859b-e3f3-6ba2-bf71-00ff98e8d5ab',
tableId: null,
position: null,
image: null,
imageName: null,
},
},
{ transaction: param }
);
}, db);
Executing (6d1aaebb-5247-4b0d-a9c4-d5b81a6da8db): START TRANSACTION;
Executing (6d1aaebb-5247-4b0d-a9c4-d5b81a6da8db): COMMIT;
Executing (default): CALL registerSportUser('email', '$2b$12$i1mc.tLpG0HjhK0Y9C/gTO1ySwBguOa2Tlr1fFZHUf1IgIKESU3gC', 1, 'firstname', 'lastName', '1988-07-17 02:00:00', 'Jobtitle', 1, false, '11ea859b-e3f3-6ba2-bf71-00ff98e8d5ab', NULL, NULL, NULL, NULL)
Unhandled rejection SequelizeDatabaseError: Column 'TabelId' cannot be null
Executing (e5fb0b6b-1340-4f9f-9bf0-f8913ba54751): ROLLBACK;
最佳答案
你忘记从 db.sequelize.query 返回 Promise :
const r = executor(param => {
return db.sequelize.query(
'CALL registerSportUser (:email, :password, :roleId, :firstName, :lastName, :age, :jobTitle, :prefLanguaged, :assigned, :sportId, :tableId, :position, :image, :imageName)',
{
replacements: {
email: args.input.Email,
password: PasswordHash,
roleId: args.input.RoleId,
firstName: args.input.FirstName,
lastName: args.input.LastName,
age: new Date(new Date(args.input.Age).toUTCString()),
jobTitle: args.input.JobTitle,
prefLanguaged: args.input.PrefLanguaged,
assigned: false,
sportId: '11ea859b-e3f3-6ba2-bf71-00ff98e8d5ab',
tableId: null,
position: null,
image: null,
imageName: null,
},
},
{ transaction: param }
);
}, db);
关于mysql - 如何在 sequelize 中执行事务和回滚?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61754750/
我是一名优秀的程序员,十分优秀!