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

Composite primary key causes issues with findOne() #1157

Closed
reramjiawan opened this issue Dec 3, 2020 · 1 comment
Closed

Composite primary key causes issues with findOne() #1157

reramjiawan opened this issue Dec 3, 2020 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@reramjiawan
Copy link

reramjiawan commented Dec 3, 2020

Describe the bug
Composite primary key causes select query syntax issues with findOne()

Stack trace

SyntaxErrorException: select `e0`.* from `d` as `e0` where (`e0`.`a_id`, `e0`.`a_c_id`) = values ('2f2837fe-ebaf-45eb-a858-2789ff675d21', 'a984c575-0bc7-4fc8-9390-849c7e60430d') limit 1 - SQLITE_ERROR: near "values": syntax error
        at SqliteExceptionConverter.convertException (C:\Users\ravin\Desktop\mikro-orm\packages\sqlite\src\SqliteExceptionConverter.js:38:20)
        at SqliteDriver.convertException (C:\Users\ravin\Desktop\mikro-orm\packages\core\src\drivers\DatabaseDriver.js:171:54)
        at C:\Users\ravin\Desktop\mikro-orm\packages\core\src\drivers\DatabaseDriver.js:175:24
        at SqliteDriver.find (C:\Users\ravin\Desktop\mikro-orm\packages\knex\src\AbstractSqlDriver.js:42:24)
        at SqliteDriver.findOne (C:\Users\ravin\Desktop\mikro-orm\packages\knex\src\AbstractSqlDriver.js:56:21)
        at SqlEntityManager.findOne (C:\Users\ravin\Desktop\mikro-orm\packages\core\src\EntityManager.js:192:22)
    
    previous Error: select `e0`.* from `d` as `e0` where (`e0`.`a_id`, `e0`.`a_c_id`) = values ('2f2837fe-ebaf-45eb-a858-2789ff675d21', 'a984c575-0bc7-4fc8-9390-849c7e60430d') limit 1 - SQLITE_ERROR: near "values": syntax error

To Reproduce

import {
  Collection,
  Entity,
  ManyToOne,
  MikroORM,
  OneToMany,
  OneToOne,
  PrimaryKey,
} from '@mikro-orm/core';
import { AbstractSqlDriver } from '@mikro-orm/knex';
import { v4 } from 'uuid';

@Entity()
export class D {

  @PrimaryKey()
  id = v4();

  @ManyToOne({ entity: 'A' })
  a!: any;

}

@Entity()
export class C {

  @PrimaryKey()
  id = v4();

}

@Entity()
export class B {

  @PrimaryKey()
  id = v4();

}

@Entity()
export class A {

  @OneToOne({ entity: 'B', joinColumn: 'id', primary: true })
  id!: B;

  @ManyToOne({ entity: 'C', primary: true })
  c!: C;

  @OneToMany({ entity: 'D', mappedBy: 'a', eager: true })
  d = new Collection<D>(this);

}

describe('GH issue 1157', () => {

  let orm: MikroORM<AbstractSqlDriver>;

  beforeAll(async () => {
    orm = await MikroORM.init({
      entities: [A, B, C, D],
      dbName: ':memory:',
      type: 'sqlite',
    });
    await orm.getSchemaGenerator().createSchema();
  });

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

  test('Composite key gives issues with select query', async () => {
    const c = orm.em.create(C, {});
    const b = orm.em.create(B, {});
    const a = orm.em.create(A, {
      id: b,
      c,
    });
    orm.em.persistAndFlush(a);

    // SyntaxErrorException
    orm.em.findOne(D, { a });
    orm.em.clear();
    // Adding expect ignores the syntax error so not adding a assert
  });
});

Expected behavior
Working findOne() with composite keys.

Versions

Dependency Version
node 12.18.4
typescript 4.0.3
mikro-orm 4.3.3-dev.17
mikro-orm/sqlite 4.3.3-dev.17
@B4nan B4nan added the bug Something isn't working label Dec 4, 2020
@B4nan B4nan closed this as completed in 0da6347 Dec 4, 2020
@B4nan
Copy link
Member

B4nan commented Dec 4, 2020

Fix will be available in 4.3.3-dev.20 in few minutes.

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