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

Regex flags does not work in where conditions #3801

Closed
daenash opened this issue Nov 29, 2022 · 2 comments
Closed

Regex flags does not work in where conditions #3801

daenash opened this issue Nov 29, 2022 · 2 comments
Labels
bug Something isn't working

Comments

@daenash
Copy link

daenash commented Nov 29, 2022

Describe the bug
The regex flags in where conditions does not work.

Expected behavior
I wanted to filter on the name of a client entity. The Client entity looks like this

import { Entity, PrimaryKey, Property } from "@mikro-orm/core";

@Entity({ tableName: "clients" })
export class Client {
  @PrimaryKey({ autoincrement: true, type: "numeric" })
  id!: number;

  @Property({ type: "varchar" })
  name!: string;
}

In the database you can find this record (returned when calling a findAll() without any further options)

const clientRepository = db.orm.em.getRepository(Client);
const regexTestResponse = await clientRepository.findAll();

// returns: 
// 
// [
//   Client {
//     id: '1',
//     name: 'Corkery and Sons',
//     projects: Collection<Project> { initialized: false, dirty: false }
//   }
// ]

However

When I wanted to filter on the name with regex using the incasesensitive (i) flag it returns nothing.

const clientRepository = db.orm.em.getRepository(Client);
const regexTestResponse = await clientRepository.find({
   name: new RegExp("corkery and sons", "i"),
});

// returns: 
//
// []

Versions

Dependency Version
node v16.14.2
typescript 4.8.4
mikro-orm 5.4.2
your-driver postgresql
@B4nan B4nan added the bug Something isn't working label Nov 29, 2022
@B4nan
Copy link
Member

B4nan commented Nov 29, 2022

You could work this around by prefixing the regex with (?i) probably:

const regexTestResponse = await clientRepository.find({
   name: { $re: "(?i)corkery and sons" },
});

https://stackoverflow.com/a/70575793/3665878

@daenash
Copy link
Author

daenash commented Nov 29, 2022

@B4nan

Thank you for the quick response! Yep it works perfectly this way. 🥳

I guess the regex handling works differently for each driver that is why it's not working correctly here, right? I also noticed that I can add a flags property if I use $re in the where condition but it also throws an error.

E.g.

const regexTestResponse = await clientRepository.find({
   name: { $re: "corkery and sons", flags: 'i' },
});
> DriverException: Invalid query condition: { name: { flags: 'i' } }

@B4nan B4nan closed this as completed in 1a1d381 Nov 29, 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

2 participants