'TYPEOrm how to apply where LIKE on entity ID
I'm using TypeORM on my application and I try to make a query filtered on multiple fields.
My entity look like this :
@Entity()
export default class Settings extends Base {
@IsString()
@IsDefined()
@MaxLength(256)
@MinLength(2)
@Column({ type: 'varchar', length: 256 })
public transport: string;
@Type(() => Beneficiary)
@OneToOne(() => Beneficiary, {
nullable: false,
eager: true,
})
@JoinColumn()
public beneficiary: Beneficiary
}
I want to filter my query on transport field with operator LIKE.
And also want to filter my query on beneficiary with operator LIKE.
Here is my query :
const [settings, totalSettings] = await getRepository(Settings).findAndCount(
{
loadRelationIds: true,
where: {
transport: Like('%bike%'),
beneficiary: Like('35'),
},
},
)
I used loadRelationIds to map the entity behind beneficiary to get the id directly rather than the complete object.
I saw that I can directly use the mapped field beneficiary like this in my where :
where: {
transport: Like('%bike%'),
beneficiary: '3',
},
But if I add the operator Like before, I got this error :
QueryFailedError: operator does not exist: integer ~~ unknown
Is there a way to add the operator LIKE to this field ?
It's look like I can't use Like on number or entity data.
Or I badly written my query.
Thanks
Solution 1:[1]
The only thing that I could think of was to use Raw typescript inside where option, so something like this:
where: {
transport: Like('%bike%'),
beneficiary: Raw((alias) => `CAST(${alias} as varchar) ILike '%${3}%'`)
},
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | aleksamarkoni |
