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

Optional properties on inverse side populated as undefined instead of null with LoadStrategy.JOINED #4675

Closed
squivix opened this issue Sep 5, 2023 · 1 comment

Comments

@squivix
Copy link

squivix commented Sep 5, 2023

Describe the bug
When fetching with populate and LoadStrategy.JOINED, a null relationship on the inverse side is populated as undefined instead of null even though I have forceUndefined set to false.

Example

User.ts

@Entity()
export class User extends CustomBaseEntity {
    @Property({type: types.string})
    @Unique()
    username!: string;

    @OneToOne({entity: () => Profile, mappedBy: (profile: Profile) => profile.user, nullable: true})
    profile!: Ref<Profile> | null;


    [OptionalProps]?: "profile";
}

Profile.ts

@Entity()
export class Profile extends CustomBaseEntity {
    @OneToOne({entity: () => User, inversedBy: (user) => user.profile, owner: true, hidden: true})
    user!: User;

    @Property({type: types.string})
    name!: string;
}

Session.ts

@Entity()
export class Session extends CustomBaseEntity {
    @Property({type: types.string, length: 255})
    token!: string;

    @ManyToOne({entity: () => User})
    user!: User;
}

Now if we have the following data in the db
users table

id username
1 user1

session table

id token user_id
1 abc123 1

profile table

id name user_id

Then we try to fetch Session populating User and its Profile:

const session = await orm.em.findOneOrFail(Session, {token: "abc123"}, {populate: ["user", "user.profile"]});
console.log(session.user.profile); //expected: null, actual: undefined

We get undefined instead of null

Expected behavior
It should be null instead of undefined.

Additional context
This is doesn't seem to happen with LoadStrategy.SELECT_IN. It also doesn't happen when I fetch directly from the entity User, only populating.

Versions

Dependency Version
node v18.12.0
typescript 4.6.4
mikro-orm 5.7.13
@mikro-orm/postgresql 5.7.13
@B4nan
Copy link
Member

B4nan commented Sep 5, 2023

This is rather expected behavior, I will see if it can be fixed somehow, but you are probably getting undefined there because the value is not hydrated at all, not because it is hydrated to undefined - as opposed to using the select-in strategy where the value is hydrated to null which comes from the database.

If you want to speed things up, a complete repro is welcome.

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