Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

One-to-One relationship is not populated in the inverse side #4879

Closed
thiagomini opened this issue Oct 26, 2023 · 4 comments
Closed

One-to-One relationship is not populated in the inverse side #4879

thiagomini opened this issue Oct 26, 2023 · 4 comments

Comments

@thiagomini
Copy link
Contributor

thiagomini commented Oct 26, 2023

Describe the bug

  • Given we have a relationship of One-to-One: 1 User has 1 Profile
  • And the User side is the inverse side (it holds the profile_id column)
  • And There is a managed user entity
  • When we create a new Profile passing the user's id as a parameter, and using the Reference.createFromPK(User, user.id) strategy
  • Then the user's profile_id is not correctly populated (and therefore, the profile_id is not updated in the database

Stack trace

 FAIL  src/mikro-orm.test.ts > Mikro Orm > creates a user and assign a profile to it (using id)
AssertionError: expected null to be truthy

- Expected: 
null

+ Received: 
false

 ❯ src/mikro-orm.test.ts:73:37
     71|       refresh: true
     72|     });
     73|     expect(userWithProfile.profile).toBeTruthy()
       |                                     ^
     74|   });
     75| });

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯

 Test Files  1 failed (1)
      Tests  1 failed | 1 passed (2)
   Start at  16:24:34
   Duration  1.38s (transform 200ms, setup 0ms, collect 119ms, tests 215ms, environment 0ms, prepare 216ms)

To Reproduce
Steps to reproduce the behavior:

  1. Clone the minimum reproducible code repository: https://github.com/thiagomini/mikro-orm-repro/tree/bug/bidirectional-relationship
  2. run yarn
  3. run docker compose up -d
  4. run yarn test

Expected behavior
the profile_id should be populated in the user side

Additional context

Here are some logs for the failing test:

stdout | src/mikro-orm.test.ts > Mikro Orm > creates a user and assign a profile to it (using id)
[query] begin

stdout | src/mikro-orm.test.ts > Mikro Orm > creates a user and assign a profile to it (using id)
[query] insert into "test"."user" ("first_name", "last_name", "email", "created_at", "updated_at") values ('firstName', 'lastName', 'email@mail.com', '2023-10-26T19:24:35.495Z', '2023-10-26T19:24:35.495Z') returning "id" [took 1 ms]
[query] commit

stdout | src/mikro-orm.test.ts > Mikro Orm > creates a user and assign a profile to it (using id)
[query] begin

stdout | src/mikro-orm.test.ts > Mikro Orm > creates a user and assign a profile to it (using id)
[query] insert into "test"."profile" ("id", "image_url", "active") values ('0', 'https://example.com', true) returning "id", "active" [took 1 ms]
[query] commit

stdout | src/mikro-orm.test.ts > Mikro Orm > creates a user and assign a profile to it (using id)
[query] select "u0".* from "test"."user" as "u0" where "u0"."id" = '1' limit 1 [took 1 ms, 1 result]

As you can see, the user.profile_id column is not updated.

Versions

Dependency Version
node 18.12.1
typescript 5.0.4
mikro-orm 5.8.4
pg 8.11.2
@thiagomini
Copy link
Contributor Author

Just one additional info, @B4nan: This test case passes when we pass the User instance instead of its id. So, the issue is probably related to the Reference.createFromPK() method.

I also believe the issue is somewhere in the defineReferenceProperties method:

set(val: AnyEntity | Reference<AnyEntity>) {

IIRC, it was not recognizing the Ref with an Id as a reference

@thiagomini
Copy link
Contributor Author

Hey again, @B4nan, sorry for bothering you with this - but could you shed some light for us on where we could look in the code to find the root cause for that? The info above was all that I was able to find out so far. Thanks, man!

@B4nan B4nan closed this as completed in 9b2c9fe Oct 31, 2023
@B4nan
Copy link
Member

B4nan commented Oct 31, 2023

FYI you dont need that conditional logic in your constructor:

this.user = ref(User, props.userOrId);

@thiagomini
Copy link
Contributor Author

Thank you very much for the fix and the hint, Martin! We truly appreciate it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants