3

I have a Mysql DataBase, and I need to make a consult for return all the relations (I'm consulting to relation table) that matches with an array of Ids, so for that I'll receive the array by body and I'm planing do the next consult:

const taskRelationFound = await getRepository(TaskRelation).findOne({ where: { ProjectId: ProjectIds , IsActive: true } });

it is into a async method, and the value of projectsIds is the next: [ "ccb79423-ed2b-4650-acb4-567c5e2a6cff", "68ff86e2-1c81-4487-bc7e-dddf1507f99e" ]

ProjectIds is an array with all the Id's where I'm looking for get the relations where each Id's matches with the ProjectId column, but I'm getting an error in the consult, and I'm not sure why

My entity works with this structure:

   TaskRelationId: string;
    TaskId: string;
    UserId: string;
    ProjectId: string;
    IsResponsable: boolean;
    IsActive: boolean;

so how can I get all relations where ProjectId Matches with any ProjectId that I'm giving throw the array?

1 Answer 1

18

As you want to apply the where clause to an array projectIds, you need to use In operator as follows -

import { In } from 'typeorm';
const taskRelationFound = await getRepository(TaskRelation).findOne({ where: { ProjectId: In(ProjectIds) , IsActive: true } });
Sign up to request clarification or add additional context in comments.

2 Comments

How to do this with query builder?
This GitHub issue link will be of help: github.com/typeorm/typeorm/issues/1239 ..It illustrates how to construct such queries using a query builder

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.