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

populate issue introduced with 5.7.x #4899

Closed
SantoJambit opened this issue Nov 6, 2023 · 4 comments
Closed

populate issue introduced with 5.7.x #4899

SantoJambit opened this issue Nov 6, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@SantoJambit
Copy link

Describe the bug
Using populate does not work as expected after an update. The last working version I found is 5.6.16. Did not try V6 yet, as it's not final.

I have the following tables:

class UserSkill {
    @PrimaryKey()
    id: number;
    
    @ManyToOne(() => User, { nullable: false })
    user: User;
    
    @ManyToOne(() => Skill, { nullable: false })
    skill: Skill;
...
}

class User {
    @PrimaryKey()
    id: string;

    @OneToMany({ entity: () => UserSkill, mappedBy: "user" })
    userSkills = new Collection<UserSkill>(this);
...
}

class Skill {
    @PrimaryKey()
    id: number;

    @Property({ nullable: false })
    label: string;

    @OneToMany(() => UserSkill, "skill")
    userSkills = new Collection<UserSkill>(this);
...
}

I left out parts that did not seem relevant.

I want to get all users that have a specified skill and list/populate all the skills each user has.
My Query was like this:

        const userSkills = await em.find(
            UserSkill,
            {
                skill: skills,
                user: {
                    accountEnabled: true,
                },
            },
            {
                populate: ["user.userSkills.skill", "user.userSkills.user"],
            }
        );

This was populating all of the user.userSkills.skill.* fields before 5.7.x, but afterwards I noticed user.userSkills.skill.label was not set. Interestingly, when I change the populate line to this, it works:

                populate: ["user.userSkills.skill", "user.userSkills.user", "skill"],

Just using "skill" doesn't work either:

                populate: ["user.userSkills.user", "skill"],

I don't care about "skill", as I already know the input skill. I care about all the other skills, which are available via user.userSkills.skill.

To Reproduce
Steps to reproduce the behavior:

  1. Reproduce the above scenario
  2. Try with 5.6.16 and then with v >= 5.7.x. Not sure which x is correct here, as versions 5.7.0 up to (and including) 5.7.11 give me a different error (TypeError: core_1.Utils.mergeConfig is not a function)

Expected behavior
Populate should work as before.

Additional context
I found an issue, which might be related, but was marked as fixed: #4339
I think it might have the same root cause, as "skill" exists in multiple places here too.

Versions

Dependency Version
node 18.13.0
typescript 4.9.4
mikro-orm 5.7.x - 5.9.1
your-driver postgresql
@B4nan
Copy link
Member

B4nan commented Nov 6, 2023

I need to see a complete reproduction for such issues, including the data, you mentioned #4339, that issue is closed with a passing test case, so ideally adjust that test so it fails.

5.7.11 give me a different error (TypeError: core_1.Utils.mergeConfig is not a function)

This means you have a version mismatch, some of your packages are newer than the core package. But let's not deal with it (it's actually already fixed in recent versions which pin the internal dependencies), let's talk about the latest stable (which is 5.9.2, not 5.9.1, but I don't think there were relevant fixes in the last release anyway), or even better about the latest 5.x dev release.


FYI nullable: false is the default.

edit: I guess you are using joined strategy? also are we talking about how entities are hydrated or how they look when serialized?

@SantoJambit
Copy link
Author

SantoJambit commented Nov 6, 2023

I have used the default loadStrategy so far. I wasn't aware of that option. I guess with postgres it makes sense to make joined the default loadStrategy?

In any way, this worked before 5.7.x with the default strategy and now it doesn't. Was it a bug that it worked before?

I've created a test-case here: https://github.com/SantoJambit/mikro-orm/blob/GH4899/tests/issues/GH4899.test.ts
It fails with default strategy and works with joined strategy.

@B4nan
Copy link
Member

B4nan commented Nov 6, 2023

Interesting, let me take a closer look.

The end goal is to have both strategies work the same.

@B4nan B4nan added the bug Something isn't working label Nov 6, 2023
@B4nan B4nan closed this as completed in 21fd312 Nov 6, 2023
@SantoJambit
Copy link
Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants