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

Nested query conditions in repository.remove cause TypeError #492

Closed
jasperroloff opened this issue Apr 19, 2020 · 3 comments
Closed

Nested query conditions in repository.remove cause TypeError #492

jasperroloff opened this issue Apr 19, 2020 · 3 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@jasperroloff
Copy link

Describe the bug
When calling the .remove(...) method of a repository with nested query conditions, it causes a TypeError.

Stack trace

(node:66382) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'slice' of undefined
    at ObjectCriteriaNode.autoJoin (PROJECT_DIR/node_modules/mikro-orm/dist/query/CriteriaNode.js:215:37)
    at ObjectCriteriaNode.process (PROJECT_DIR/node_modules/mikro-orm/dist/query/CriteriaNode.js:168:26)
    at PROJECT_DIR/node_modules/mikro-orm/dist/query/CriteriaNode.js:172:39
    at Array.reduce (<anonymous>)
    at ObjectCriteriaNode.process (PROJECT_DIR/node_modules/mikro-orm/dist/query/CriteriaNode.js:170:42)
    at QueryBuilder.init (PROJECT_DIR/node_modules/mikro-orm/dist/query/QueryBuilder.js:274:99)
    at QueryBuilder.delete (PROJECT_DIR/node_modules/mikro-orm/dist/query/QueryBuilder.js:54:21)
    at MariaDbDriver.nativeDelete (PROJECT_DIR/node_modules/mikro-orm/dist/drivers/AbstractSqlDriver.js:95:69)
    at EntityManager.nativeDelete (PROJECT_DIR/node_modules/mikro-orm/dist/EntityManager.js:186:39)
    at EntityManager.remove (PROJECT_DIR/node_modules/mikro-orm/dist/EntityManager.js:297:21)
    at EntityRepository.remove (PROJECT_DIR/node_modules/mikro-orm/dist/entity/EntityRepository.js:36:24)
    at PROJECT_DIR/src/main.ts:33:47
    at Generator.next (<anonymous>)
    at fulfilled (PROJECT_DIR/src/main.ts:4:58)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:66382) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
(node:66382) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

To Reproduce
I've created a sample repository to reproduce this issue, use ts-node src/main.ts to run.
https://github.com/jasperroloff/mikro-orm-nested-query-delete-issue

Expected behavior
It should successfully delete the entity, since a findOne given the same query does succeed.

Additional context
I've experienced this issue with (at least) the mariadb driver and the ReflectMetadataProvider.

Versions

Dependency Version
node v13.13.0
typescript ^3.8.3
mikro-orm ^3.6.8
mariadb ^2.3.1
mysql2 ^2.1.0
ts-node ^8.8.2

(see package.json in sample repo)

@jasperroloff jasperroloff added the bug Something isn't working label Apr 19, 2020
@jasperroloff jasperroloff changed the title Nested query conditions in repository.remove causes TypeError Nested query conditions in repository.remove cause TypeError Apr 19, 2020
@B4nan
Copy link
Member

B4nan commented Apr 19, 2020

This was never meant for update or delete queries. Will see if I can make this work in v4, if nothing better, we could at least do 2 queries under the hood (select PK and delete by PK).

As a workaround, select the entity as usual and remove the reference you get.

@B4nan B4nan added enhancement New feature or request and removed bug Something isn't working labels Apr 19, 2020
@B4nan B4nan added this to the 4.0 milestone Apr 19, 2020
@jasperroloff
Copy link
Author

Thanks for your reply, I thought MikroORM is supporting this already and I just found a bug.
The workaround you mentioned, I am using already.

So in this case, consider this more as a feature request than a bug 😄

@B4nan B4nan mentioned this issue Apr 30, 2020
46 tasks
B4nan added a commit that referenced this issue May 2, 2020
```typescript
qb.delete({ books: { author: 123 } });`
```

will result in following query:

```sql
delete from `publisher2` where `id` in (select `e0`.`id` from (
  select distinct `e0`.`id` from `publisher2` as `e0` left join `book2` as `e1` on `e0`.`id` = `e1`.`publisher_id` where `e1`.`author_id` = ?
) as `e0`)
```

Closes #492
B4nan added a commit that referenced this issue May 2, 2020
```typescript
qb.delete({ books: { author: 123 } });`
```

will result in following query:

```sql
delete from `publisher2` where `id` in (select `e0`.`id` from (
  select distinct `e0`.`id` from `publisher2` as `e0` left join `book2` as `e1` on `e0`.`id` = `e1`.`publisher_id` where `e1`.`author_id` = ?
) as `e0`)
```

Closes #492
@B4nan
Copy link
Member

B4nan commented May 2, 2020

Closing as implemented via #538, which is now merged in v4 (dev branch). Subscribe here for updates #527.

@B4nan B4nan closed this as completed May 2, 2020
B4nan added a commit that referenced this issue May 3, 2020
```typescript
qb.delete({ books: { author: 123 } });`
```

will result in following query:

```sql
delete from `publisher2` where `id` in (select `e0`.`id` from (
  select distinct `e0`.`id` from `publisher2` as `e0` left join `book2` as `e1` on `e0`.`id` = `e1`.`publisher_id` where `e1`.`author_id` = ?
) as `e0`)
```

Closes #492
B4nan added a commit that referenced this issue May 21, 2020
```typescript
qb.delete({ books: { author: 123 } });`
```

will result in following query:

```sql
delete from `publisher2` where `id` in (select `e0`.`id` from (
  select distinct `e0`.`id` from `publisher2` as `e0` left join `book2` as `e1` on `e0`.`id` = `e1`.`publisher_id` where `e1`.`author_id` = ?
) as `e0`)
```

Closes #492
B4nan added a commit that referenced this issue Jun 1, 2020
```typescript
qb.delete({ books: { author: 123 } });`
```

will result in following query:

```sql
delete from `publisher2` where `id` in (select `e0`.`id` from (
  select distinct `e0`.`id` from `publisher2` as `e0` left join `book2` as `e1` on `e0`.`id` = `e1`.`publisher_id` where `e1`.`author_id` = ?
) as `e0`)
```

Closes #492
B4nan added a commit that referenced this issue Jun 5, 2020
```typescript
qb.delete({ books: { author: 123 } });`
```

will result in following query:

```sql
delete from `publisher2` where `id` in (select `e0`.`id` from (
  select distinct `e0`.`id` from `publisher2` as `e0` left join `book2` as `e1` on `e0`.`id` = `e1`.`publisher_id` where `e1`.`author_id` = ?
) as `e0`)
```

Closes #492
B4nan added a commit that referenced this issue Jun 16, 2020
```typescript
qb.delete({ books: { author: 123 } });`
```

will result in following query:

```sql
delete from `publisher2` where `id` in (select `e0`.`id` from (
  select distinct `e0`.`id` from `publisher2` as `e0` left join `book2` as `e1` on `e0`.`id` = `e1`.`publisher_id` where `e1`.`author_id` = ?
) as `e0`)
```

Closes #492
B4nan added a commit that referenced this issue Aug 2, 2020
```typescript
qb.delete({ books: { author: 123 } });`
```

will result in following query:

```sql
delete from `publisher2` where `id` in (select `e0`.`id` from (
  select distinct `e0`.`id` from `publisher2` as `e0` left join `book2` as `e1` on `e0`.`id` = `e1`.`publisher_id` where `e1`.`author_id` = ?
) as `e0`)
```

Closes #492
B4nan added a commit that referenced this issue Aug 9, 2020
```typescript
qb.delete({ books: { author: 123 } });`
```

will result in following query:

```sql
delete from `publisher2` where `id` in (select `e0`.`id` from (
  select distinct `e0`.`id` from `publisher2` as `e0` left join `book2` as `e1` on `e0`.`id` = `e1`.`publisher_id` where `e1`.`author_id` = ?
) as `e0`)
```

Closes #492
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants