简体   繁体   中英

sequalize create record transaction

I have three models:

    var Person = sequelize.define("Person", {
        no: {type: DataTypes.STRING, unique: true, allowNull: false}
    }, {
        classMethods: {
            associate: function(models){
                Person.hasMany(models.Task);
                Person.hasMany(models.Job);
            }
        }
    });

Task has field: description, and Job has field: title.

Now I try to use transaction to insert record, but I am stuck:

return sequalize.transaction(function (t) {
    return models.Person.create({
        no: '1221212'
    }, {transaction: t}).then(function (person){
        // I am stuck here, to insert multiple task and job
    }, {transaction: t});
}).then(function(result){

}).catch(function(err){

});

How can I insert multiple task and job after create a person?

Option 1

You can create child objects like this:

return sequelize.transaction(function (t) {
    return Person.create({
            no: '1221212',
            Tasks: [
                { taskName: 'task1' },
                { taskName: 'task2' }
            ]}, {
                transaction: t,
                include: [ { model: Task  } ]
            }
    );
});

We use "include" option here.

Here is SQL generated:

Executing (61834d07-de9e-4b6e-aa53-9cead8157595): START TRANSACTION;
Executing (61834d07-de9e-4b6e-aa53-9cead8157595): SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
Executing (61834d07-de9e-4b6e-aa53-9cead8157595): SET autocommit = 1;
Executing (61834d07-de9e-4b6e-aa53-9cead8157595): INSERT INTO "People" ("id","no") VALUES (DEFAULT,'1221212') RETURNING *;
Executing (61834d07-de9e-4b6e-aa53-9cead8157595): INSERT INTO "Tasks" ("id","taskName","PersonId") VALUES (DEFAULT,'task1',3) RETURNING *;
Executing (61834d07-de9e-4b6e-aa53-9cead8157595): INSERT INTO "Tasks" ("id","taskName","PersonId") VALUES (DEFAULT,'task2',3) RETURNING *;
Executing (61834d07-de9e-4b6e-aa53-9cead8157595): COMMIT;

Option 2

You can also create a Person and then add Tasks to it in a bulk. This will work good if number of tasks is big, because it use only one INSERT operation for all tasks.

return sequelize.transaction(function (t) {
    return Person.create({
            no: '00000'
        }, {
            transaction: t,
        })
    .then(function(person) {
        return Task.bulkCreate( [
                { taskName: 't1', PersonId: person.id },
                { taskName: 't2', PersonId: person.id }
            ], {
                transaction: t
            });
    });
});

We use bulkCreate function and manually set id for the foreign key field. This will generate the following SQL:

Executing (c9802841-6a22-4633-a006-41a78a9623b2): START TRANSACTION;
Executing (c9802841-6a22-4633-a006-41a78a9623b2): SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
Executing (c9802841-6a22-4633-a006-41a78a9623b2): SET autocommit = 1;
Executing (c9802841-6a22-4633-a006-41a78a9623b2): INSERT INTO "People" ("id","no") VALUES (DEFAULT,'00000') RETURNING *;
Executing (c9802841-6a22-4633-a006-41a78a9623b2): INSERT INTO "Tasks" ("id","taskName","PersonId") VALUES (DEFAULT,'t1',4),(DEFAULT, 't2', 4)
Executing (c9802841-6a22-4633-a006-41a78a9623b2): COMMIT;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM