Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.8k views
in Technique[技术] by (71.8m points)

node.js - Transaction doesn't work as expected in sequelize - Node js

I have a child table where I have to fill in the data. I am using transactions and sequelize in node js.

The data in the child table just saves the default columns. The syntax is all good because when I save just the child row using the same code, it saves the data correctly (disabled FK constraints).

var transaction;
    try {
        await authUtil.validateToken(req);
        await authUtil.mustBePmllMember(req);
        transaction = await db.sequelize.transaction();
        console.log(req.body);

        var workId = req.params.workId;
        if (!workId) {
            logger.error("Invalid workId -> " + workId);
            throw {
                errorCode: 400,
                errorMessage: constants.INVALID_PATH_PARAMS
            };
        }

        var data = {
            'work_id': workId,
            'new_song_id': req.body.song_id,
            'new_work_title': req.body.song_title,
            'new_publication_id': req.body.publication_id,
            'new_publication_title': req.body.publication_name,
            'new_isbn': req.body.isbn,
            'new_print_publisher_id': req.body.print_publisher_id,
            'new_composer_name': req.body.composer_name,
            'new_print_publisher': req.body.publisher_name,
            'new_page_from': req.body.page_from,
            'new_page_to': req.body.page_to,
            'status': constants.AMENDMENT_STATUS_ACTIVE,
            'created_by': req.header('id')
        }

        var amendment = await Amendments.create(data, transaction);
        console.log(amendment.id);
        var musicPublishers = req.body.music_publishers;
       
        for (var i = 0; i < musicPublishers.length; i++) {
            console.log(musicPublishers[i].id);
            console.log(musicPublishers[i].name);
            var musicPublisherDetails = {
                'amendment_id': amendment.id,
                'music_publisher_id': musicPublishers[i].id,
                'music_publisher_name': musicPublishers[i].name,
                'created_by': req.header('id')
            }
            console.log(musicPublisherDetails);
            await AmendmentsMusicPublishers.create(musicPublisherDetails, transaction);
        }

        await transaction.commit();
        res.status(201).send();
        return;
    } catch (err) {
        if (transaction) await transaction.rollback();
        logger.error("error in getting work usage %o", err);
        console.log(err);
        await exceptionUtil.sendException(res, err, 500, constants.GENERAL_ERROR)
        return;
    }

This is how the db looks after the exection.

enter image description here

And this is my sample input that I am using as POST for this API.

{
        "song_id": 1,
        "song_title": "song abc",
        "publication_id": 1,
        "publication_name": "nw publication name",
        "isbn": "1",
        "print_publisher_id": 1,
        "composer_name": "c name",
        "publisher_name": "new pname",
        "page_from": 2,
        "page_to": 3,
        "music_publishers": [
            {"id":"1",
            "name":"pub name"
            },
            {"id":"",
            "name":"pub name"
            }
            
        ]
    }

I can see in the logs, the publication name and id are not passed in the query generated.

Executing (88a998b1-a4ce-48a9-a705-6f79275638c2): START TRANSACTION;
{ song_id: 1,
  song_title: 'song abc',
  publication_id: 1,
  publication_name: 'nw publication name',
  isbn: '1',
  print_publisher_id: 1,
  composer_name: 'c name',
  publisher_name: 'new pname',
  page_from: 2,
  page_to: 3,
  music_publishers: [ { id: '1', name: 'pub name' }, { id: '', name: 'pub name' } ] }
Executing (default): INSERT INTO `amendments` (`id`,`work_id`,`new_work_title`,`new_song_id`,`new_publication_id`,`new_publication_title`,`new_isbn`,`new_print_publisher_id`,`new_print_publisher`,`new_page_from`,`new_page_to`,`new_composer_name`,`status`,`created_at`,`created_by`,`updated_at`) VALUES (DEFAULT,?,?,?,?,?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP,?,CURRENT_TIMESTAMP);
2
1
pub name
{ amendment_id: 2,
  music_publisher_id: '1',
  music_publisher_name: 'pub name',
  created_by: '15' }
Executing (default): INSERT INTO `amendments_music_publishers` (`id`,`created_at`,`created_by`,`updated_at`) VALUES (DEFAULT,CURRENT_TIMESTAMP,?,CURRENT_TIMESTAMP);

pub name
{ amendment_id: 2,
  music_publisher_id: '',
  music_publisher_name: 'pub name',
  created_by: '15' }
Executing (default): INSERT INTO `amendments_music_publishers` (`id`,`created_at`,`created_by`,`updated_at`) VALUES (DEFAULT,CURRENT_TIMESTAMP,?,CURRENT_TIMESTAMP);
Executing (88a998b1-a4ce-48a9-a705-6f79275638c2): COMMIT;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...