Skip to content

QueryBuilder with leftJoinAndSelect and pessimistic_write lock (postgres) #4084

@GitStorageOne

Description

@GitStorageOne

Issue type:

[ ] question
[ ] bug report
[x] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[x] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[ ] latest
[ ] @next
[x] 0.2.16 (or put your version here)

Steps to reproduce or a small repository showing the problem:

.createQueryBuilder(User, 'user')
  .setLock('pessimistic_write')
  .leftJoinAndSelect('user.someAnotherTable', 'someAnotherTable')
  .where('email LIKE :email', { email: 'email@mail.com' })
  .getOne();

Code above generates query:

SELECT "user"."id" AS "user_id",
       "someAnotherTable"."id" AS "someAnotherTable_id"
FROM "public"."users" "user"
LEFT JOIN "public"."some_another_table" "someAnotherTable" ON "someAnotherTable"."id"="user"."some_another_table_id"
WHERE email LIKE 'email@mail.com'
  FOR UPDATE

Which produce postgresql error:

ERROR: FOR UPDATE cannot be applied to the nullable side of an outer join

Looks like DBMS trying to lock someAnotherTable as well, but typescript code above does not imply that every joined table should be locked (isn't it?).
If so, generated sql code should include part FOR UPDATE OF <> instead of FOR UPDATE:

SELECT "user"."id" AS "user_id",
       "someAnotherTable"."id" AS "someAnotherTable_id"
FROM "public"."users" "user"
LEFT JOIN "public"."some_another_table" "someAnotherTable" ON "someAnotherTable"."id"="user"."some_another_table_id"
WHERE email LIKE 'email@mail.com'
  FOR UPDATE OF "user"

similar issue:
https://code.djangoproject.com/ticket/28010

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions