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

Query builder nested relations not populating when parent exists in identity map #4173

Closed
ThatsARapp opened this issue Mar 28, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@ThatsARapp
Copy link

ThatsARapp commented Mar 28, 2023

Describe the bug
Relations two levels deep are not selected if there parent already exists in the identity map when using the query builder.

To Reproduce

import {
  Collection,
  Entity,
  ManyToOne,
  OneToMany,
  PrimaryKey,
} from '@mikro-orm/core';
import { MikroORM } from '@mikro-orm/sqlite';

@Entity()
export class User {
  @PrimaryKey()
  id!: string;

  @OneToMany(() => Post, post => post.user)
  posts = new Collection<Post>(this);
}

@Entity()
export class Post {
  @PrimaryKey()
  id!: string;

  @OneToMany(() => Comment, comment => comment.post)
  comments = new Collection<Comment>(this);

  @ManyToOne()
  user: User;
}

@Entity()
class Comment {
  @PrimaryKey()
  id!: string;

  @ManyToOne()
  post: Post;
}

let orm: MikroORM;

beforeAll(async () => {
  orm = await MikroORM.init({
    dbName: ':memory:',
    entities: [Comment, Post, User],
  });
  await orm.schema.refreshDatabase();
});

afterAll(() => orm.close(true));

beforeEach(async () => {
  const fork = orm.em.fork();
  await orm.schema.clearDatabase();
  fork.create(User, {
    id: 'user1',
  });
  fork.create(Post, {
    id: 'post1',
    user: 'user1',
  });
  fork.create(Comment, {
    id: 'comment',
    post: 'post1',
  });
  await fork.flush();
});

test('it should select comments even if posts have already been selected', async () => {
  const fork = orm.em.fork();
  await fork.qb(Post).select('*');
  const users = await fork
    .qb(User)
    .select('*')
    .joinAndSelect('posts', 'p')
    .leftJoinAndSelect('p.comments', 'c');
  expect(users[0].posts[0].comments[0]).toBeDefined();
});

//This one works without the initial   await fork.qb(Post).select('*');
test('it should select comments even if posts have already been selected', async () => {
  const fork = orm.em.fork();
  const users = await fork
    .qb(User)
    .select('*')
    .joinAndSelect('posts', 'p')
    .leftJoinAndSelect('p.comments', 'c');
  expect(users[0].posts[0].comments[0]).toBeDefined();
});

Expected behavior
Comments are returned as a child of posts

Versions

Dependency Version
node 16.19.1
typescript 4.7.4
mikro-orm 5.6.15
@B4nan B4nan added the bug Something isn't working label Mar 28, 2023
@B4nan B4nan closed this as completed in 493d653 Apr 2, 2023
jsprw pushed a commit to jsprw/mikro-orm-full-text-operators that referenced this issue May 7, 2023
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