Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

LockMode fails with eager relations#6412

Heniker started this conversation inGeneral
Discussion options

Describe the bug

Using eager relations withlockMode: LockMode.PESSIMISTIC_WRITE (or probably any otherlockMode) results in the following error:

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

Failing query example

select "u0".*, "s1"."id" as "s1__id", "s1"."surname" as "s1__surname" from "user" as "u0" left join "user2" as "s1" on "u0"."second_user_id" = "s1"."id" where "u0"."email" = 'foo' limit 1 for update - FOR UPDATE cannot be applied to the nullable side of an outer join

Reproduction

import{Entity,LockMode,ManyToOne,MikroORM,OneToMany,PrimaryKey,Property}from'@mikro-orm/postgresql';@Entity()classUser2{  @PrimaryKey()id!:number;  @Property()surname!:string}@Entity()classUser{  @PrimaryKey()id!:number;  @Property()name:string;  @Property({unique:true})email:string;  @ManyToOne(()=>User2,{eager:true})secondUser!:User2constructor(name:string,email:string){this.name=name;this.email=email;}}letorm:MikroORM;beforeAll(async()=>{orm=awaitMikroORM.init(...);awaitorm.schema.refreshDatabase();});afterAll(async()=>{awaitorm.close(true);});test('basic CRUD example',async()=>{orm.em.create(User,{name:'Foo',email:'foo',secondUser:{surname:'test'}});awaitorm.em.flush();orm.em.clear();constf=orm.em.fork()awaitf.begin()constuser=awaitf.findOneOrFail(User,{email:'foo'},{lockMode:LockMode.PESSIMISTIC_WRITE});});

What driver are you using?

@mikro-orm/postgresql

MikroORM version

6.4.3

Node.js version

.

Operating system

No response

Validations

You must be logged in to vote

Replies: 5 comments

Comment options

The problem is your populated relation, you need to lock only the root table via lockTableAliases option.

https://mikro-orm.io/docs/transactions#pessimistic-locking

You must be logged in to vote
0 replies
Comment options

Huh. My bad, missed that.

You must be logged in to vote
0 replies
Comment options

The support right now is not perfect, I'd like to provide something likelockPaths option where you wouldn't need to depend on the aliases directly (since they are dynamic, especially for relations). But this will likely still require explicit configuration.

You must be logged in to vote
0 replies
Comment options

My current workaround looks like this:

constuser=awaitdb.findOneOrFail(User,{id:1})awaitdb.lock(user,LockMode.PESSIMISTIC_WRITE)

It generates 2 queries, but w/e - it seems to work fine.

Now that I think about it, I feel like just locking the main table (User) by default would solve my use-case and be pretty much the expected behavior.

You must be logged in to vote
0 replies
Comment options

B4nan
Nov 1, 2025
Maintainer

Next version will skip left-joined tables from pessimistic locking automatically:ffb71a1

You must be logged in to vote
0 replies
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
General
Labels
None yet
2 participants
@Heniker@B4nan
Converted from issue

This discussion was converted from issue #6411 on February 12, 2025 11:16.


[8]ページ先頭

©2009-2025 Movatter.jp