0

I have tried following to find item in a array

import {Any} from "typeorm";

const loadedPosts = await connection.getRepository(Post).find({
    countries: Any(["Sri Lanka", "Nepal"])
});

but I am getting this error [QueryFailedError] could not find array type for data type text[]

my entity column define like this

@IsNotEmpty()
@Column("text", {array: true})
public countries: string[];

1 Answer 1

2

If I understand your question correctly, you have a Post entity with a countries column of type text[]. You then try to find posts that have at least one of the following countries: "Sri Lanka" or "Nepal".

This is not a TypeORM issue. It is currently not possible in PostgreSQL to find any records that have these kind of intersections using two arrays and the ANY() function. Instead try to use an array operator, such as @>.

Please have a look at the last part of Sudharsan Thumatti's answer in the following similar question: https://stackoverflow.com/a/54069718/3298175

Please note that array functions aren't always performant as someone might expect. Perhaps you are better off with a second table called countries that has a foreign key to posts in stead, creating a one-to-many relation. A junction table could help as well if you would prefer a many-to-many relation, allowing you to reuse the countries table.

Sign up to request clarification or add additional context in comments.

2 Comments

but according to documentations they have Any option github.com/typeorm/typeorm/blob/master/docs/…
That’s correct, but you can’t use that on a field that already is an array. Right now you are checking if there is an array that equals one of the following texts. That comparison is flawed and results in an error. It would work if the Post’s field would be a single country of type text, but it doesn’t work on arrays.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.