Repository navigation
Description
What are you doing?
My setup is an AWS Aurora postgres cluster with read replicas.
I use the code below to upsert and return the instance. Because my setup uses read and write pools, the first query - create or update runs on the master and the second query, the one that returns the instance(returning: true) runs on the read replica. In the case of create it returns null as the instance and in the case of update it returns a stale copy of the instance.
I think that the reason is replication lag between the master and the slaves. A good and possible solution will be to accept useMaster for the upsert query.
const [instance, created] = await Repository.upsert(
{
url: body.url,
provider: body.provider,
owner: body.owner,
name: body.name
},
{ returning: true }
)
if (created) ctx.status = 201
else ctx.status = 200
ctx.body = instanceWhat do you expect to happen?
The instance should be the one created by the query
What is actually happening?
The instance is null
Executing (default): CREATE OR REPLACE FUNCTION pg_temp.sequelize_upsert(OUT created boolean, OUT primary_key text) AS $func$ BEGIN INSERT INTO "repositories" ("name","owner","provider","url","created_at","updated_at") VALUES ('jbjkbjbff','kululu','bb','https://github.com/hababaa','2018-03-22 13:08:32.610 +00:00','2018-03-22 13:08:32.610 +00:00') RETURNING "id" INTO primary_key; created := true; EXCEPTION WHEN unique_violation THEN UPDATE "repositories" SET "url"='https://github.com/hababaa',"provider"='bb',"owner"='kululu',"name"='jbjkbjbff',"updated_at"='2018-03-22 13:08:32.610 +00:00' WHERE ("id" IS NULL OR ("name" = 'jbjkbjbff' AND "owner" = 'kululu' AND "provider" = 'bb')) RETURNING "id" INTO primary_key; created := false; END; $func$ LANGUAGE plpgsql; SELECT * FROM pg_temp.sequelize_upsert();
Executing (default): SELECT "id", "name", "owner", "provider", "url", "created_at", "updated_at" FROM "repositories" AS "Repository" WHERE "Repository"."id" = '1893';
Dialect: postgres
Dialect version:
Database version: Aurora PostgreSQL 9.6.3
Sequelize version: 4.37.4
Tested with latest release: 4.37.4
Note : Your issue may be ignored OR closed by maintainers if it's not tested against latest version OR does not follow issue template.
Activity
sushantdhiman commentedon Mar 22, 2018
Yeah
useMastercan solve this issue.For permanent solution
useMasterin find callI think (1) is good option as upsert operation itself is atomic (single query passed to engine), later on we do find which should by default read from master.