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

find queries with embeddables fail if eager relation loaded with with JOINED strategy #2717

Closed
oznakn opened this issue Feb 7, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@oznakn
Copy link

oznakn commented Feb 7, 2022

Describe the bug

em.find queries fail if an entity that includes both an embeddable and eager one-to-many relationship used.

Stack trace

TypeError: Cannot read properties of undefined (reading '0')
    at SqliteDriver.mapPropToFieldNames (.../node_modules/@mikro-orm/knex/AbstractSqlDriver.js:496:97)
    at .../node_modules/@mikro-orm/knex/AbstractSqlDriver.js:473:44
    at Array.forEach (<anonymous>)
    at SqliteDriver.getFieldsForJoinedLoad (.../node_modules/@mikro-orm/knex/AbstractSqlDriver.js:473:8)
    at SqliteDriver.buildFields (.../node_modules/@mikro-orm/knex/AbstractSqlDriver.js:612:24)
    at SqliteDriver.find (.../node_modules/@mikro-orm/knex/AbstractSqlDriver.js:27:25)
    at SqlEntityManager.find (.../node_modules/@mikro-orm/core/EntityManager.js:105:43)
    at async main (.../src/index.ts:29:15)

To Reproduce
In models.ts:

import { Collection, Embeddable, Embedded, Entity, IdentifiedReference, ManyToOne, OneToMany, PrimaryKey, Property } from "@mikro-orm/core";

@Entity()
export class Cat {
    @PrimaryKey()
    name!: string;

    @ManyToOne()
    user!: IdentifiedReference<User>;
}

@Embeddable()
export class Profile {
    @Property()
    phoneNumber?: string;
}

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

    @Property()
    name!: string;

    @Embedded({ hidden: true })
    profile?: Profile;

    @OneToMany(() => Cat, c => c.user, { eager: true })
    cats = new Collection<Cat>(this);
}

In index.ts:

import { LoadStrategy, MikroORM } from '@mikro-orm/core';
import { Cat, Profile, User } from './user';
import { TsMorphMetadataProvider } from '@mikro-orm/reflection';

async function main() {
  const orm = await MikroORM.init({
    dbName: ':memory:',
    type: 'sqlite',
    entities: [User, Profile, Cat],
    loadStrategy: LoadStrategy.JOINED,
    metadataProvider: TsMorphMetadataProvider,
  });
  await orm.getSchemaGenerator().execute(await orm.getSchemaGenerator().generate());

  const em = orm.em.fork();

  em.create(
    User,
    {
      id: 'TestPrimary',
      name: 'TestName',
    },
    { persist: true },
  );
  await em.flush();
  em.clear();

  console.log(await em.find(User, {})); // This line fails
}

main();

The find query fails if the above configuration is used.

Expected behavior
We should not see any errors.

Additional context
A reproducible repository can be found here.

Versions

Dependency Version
node 16.13.1
typescript 4.5.5
mikro-orm 5.0.0
your-driver sqlite (5.0.0)
@MikeBKemp
Copy link

I also have this issue (sqlite 5.0.0, mikro-orm 5.0.0). Having an eager 1:m or not doesn't seem to make a difference though.

@B4nan B4nan added the bug Something isn't working label Feb 7, 2022
@B4nan B4nan closed this as completed in adaa5c6 Feb 7, 2022
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

3 participants