Learning today node I want to use transaction processing result sequelize The log of shows rollback But in fact, the inserted records still exist
The configuration and code are as follows
//groups.js Here is the model definition
const Sequelize = require('sequelize');
module.exports = function(sequelize, DataTypes) {
return sequelize.define('groups', {
group_id: {
autoIncrement: true,
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
comment: " Group id"
},
user_id: {
type: DataTypes.INTEGER,
allowNull: false,
comment: " Group leader id"
},
name: {
type: DataTypes.STRING(20),
allowNull: true,
comment: " Group name "
},
img_url: {
type: DataTypes.STRING(20),
allowNull: true,
comment: " Group heads "
},
intro: {
type: DataTypes.TEXT,
allowNull: false,
comment: " Group announcement "
},
last_time: {
type: DataTypes.BIGINT,
allowNull: true,
comment: " Last communication time "
},
time: {
type: DataTypes.BIGINT,
allowNull: false,
defaultValue: 1642435200000,
comment: " Creation time "
}
}, {
sequelize,
tableName: 'groups',
timestamps: false,
indexes: [
{
name: "PRIMARY",
unique: true,
using: "BTREE",
fields: [
{ name: "group_id" },
]
},
]
});
};
// Here is the model call method
var DataTypes = require("sequelize").DataTypes;
var _groups = require("./groups");
function models(sequelize) {
var groups = _groups(sequelize, DataTypes);
return {
groups
};
}
module.exports = models;
// Here is the actual interface
/**
* @func createGroup()
* @desc Create groups
* @param {user_id: Group leader id,name: Group name ,img_url: Group heads ,t: Business }
* @return {object:groupList||object:error}
*/
exports.createGroup = async function (user_id, name, img_url) {
// Store transactions into variables
const t = await sequelize.transaction();
try {
// Pass in the connection instance and get the initialization model
let result = await models(sequelize).groups.create({
user_id: user_id,
name: name,
img_url: img_url
},{ transaction: t });
// Rollback directly for method Try a variety of writing The data still exists
await t.rollback();
console.log(' Roll back ');
return func.resJsonSuccess(result, ' Group building succeeded !');
} catch (error) {
return func.resJsonError([], error.message);
}
}
As you can see below sequelize It has been output on the console ROLLBACK But in fact, the inserted data still exists