Tuesday 6 June 2017

Sequelize findOrCreate generates two rows

I'm using Sequelize on a Postgresql database and I run the following query:

let idUser = req.user.idUser;

db.table.findOrCreate( { where : { idUser : idUser }}).spread(function (data, created) {
    res.json({
        value : data.birthday
    });
});

Given that I'm using findOrCreate, I would expect that the first time I run this on a new user, it tries to find a value in table, doesn't find it and creates a row. Any subsequent time it runs, it will find and use that row.

However, around 65% of the time it creates two rows for the same user! I store when the row was created, and the value for both rows is usually within a few milliseconds of each other. Any idea on what could be causing this? And why it happens only sometimes?

My understanding was that this wouldn't be possible when using findOrCreate. I appreciate all help! Thank you

Also, what would be the equivalent in SQL to findOrCreate?



via leofontes

No comments:

Post a Comment