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

Multiple ? in full text fields updates produce incorrect SQL #4484

Closed
tduenez opened this issue Jun 26, 2023 · 0 comments
Closed

Multiple ? in full text fields updates produce incorrect SQL #4484

tduenez opened this issue Jun 26, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@tduenez
Copy link

tduenez commented Jun 26, 2023

Describe the bug
Updating full text fields with values containing multiple question marks is producing incorrect SQL. It seems the first question mark is being escaped, but additional ones are not.

Stack trace

console.log
    [query] update "book" set "title" = 'My Life on The ? Wall, part ? ?', "searchable_title" = to_tsvector('simple', 'My Life on The ? Wall, part 1 NULL') where "id" = NULL [took 1 ms]

      at DefaultLogger.log (packages/core/src/logging/DefaultLogger.ts:34:10)

To Reproduce
Steps to reproduce the behavior:

  1. add the following test to tests/features/fulltext/full-text-search-tsvector.postgres.test.ts
test('update entity', async () => {
    const repo = orm.em.getRepository(Book);
    const book1 = new Book('My Life on The Wall');
    repo.create(book1);
    await repo.flush();

    const newTitle = 'My Life on The ? Wall, part ? ?';
    book1.title = newTitle;
    await repo.flush();
    repo.getEntityManager().clear();

    const reloadedBook = await repo.findOne({ id: book1.id });
    expect(reloadedBook?.title).toBe(newTitle);
  });

Expected behavior
The provided test to pass. Every ? should be handled correctly, not just the first one. The generated SQL should look like:

console.log
    [query] update "book" set "title" = 'My Life on The ? Wall, part ? ?', "searchable_title" = to_tsvector('simple', 'My Life on The ? Wall, part ? ?') where "id" = 1 [took 1 ms]

      at DefaultLogger.log (packages/core/src/logging/DefaultLogger.ts:34:10)

Additional context
Seems like the same issue described in #3457 but regarding multiple instances of ?.

Looking at the solution for that issue, a replacement for the first occurrence of ? was added in packages/knex/src/query/QueryBuilderHelper.ts

Seems like using a global regexp in QueryBuilderHelper.ts fixes the issue: sql.replace(/\?/g, '\\?')

Versions

Dependency Version
node v18.15.0
typescript 5.1.3
mikro-orm 5.7.12
@mikro-orm/postgresql 5.7.12
@B4nan B4nan added the bug Something isn't working label Jul 14, 2023
@B4nan B4nan closed this as completed in 9c9915e Jul 14, 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