78

I have a column kid_ages which is Integer[]. When migrating, I get the following error:

DataTypeNotSupportedError: Data type "Array" in "home.kid_ages" is not supported by "postgres" database.

I tried adding the following options to my column:

type: 'array'

and:

array: true,
default: [],
nullable: false,

like this:

@Column({
  array: true,
  default: [],
  nullable: false,
})
kid_ages: string;

8 Answers 8

127

The docs says, that it should work:

@Column("int", { array: true })
array: number[];

this is from the example https://github.com/typeorm/typeorm/blob/master/test/functional/database-schema/column-types/postgres/entity/Post.ts

In your code the array property is no array. Have you tried kid_ages: string[];?

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

2 Comments

hmm, I seem to be getting an error using this: " You need to provide explicit type" Im using the following nullable type: @Field({ nullable: true }) @Column('text', { array: true }) tags?: string[] | null
I am no TypeORM Expert but you could try to default the null value to an empty array @Column({ type: "text", array: true, default: [] })
48

For people looking to deal with arrays or string within your entities, based on @thopaw 's answer, you can use the following code:

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

Comments

24

As mentioned above, you can create Postgres' array column with next code:

@Column("int", { array: true })
kid_ages: number[];

If you then need to find some kid with age 5, use this:

kid = getRepository('kid')
        .createQueryBuilder()
        .where(':kid_age = ANY (kid.kid_ages)', { kid_age: 5 });

1 Comment

actually today (^0.3.0) it's just enough to use this one: repository.find({ where: { kid_ages: ArrayContains([5]), }}
14

I tried all of the solutions already mentioned, but none of them worked. Finally found solution among the Github issues in TypeORM repo.

I quote the solution:

This was a simple fix, the type of the column shouldn't be an array. Changed to this and it now works:

@Column({array: true})
tags: string;

Better documentation on arrays would have been useful.

Source

Comments

12

My solution for Postgres for checking array inclusion is as follows:

I have defined the column as follows:

    @Column("text", { array: true, default: "{}" })
    tags: string[];
this.getFindQueryBuilder().where("recipe.tags && ARRAY[:...tags]", {tags: tags})

"getFindQueryBuilder": Is a function to get shared SelectQueryBuilder in order to save code.

The important part here is the where clause that I have shown in the example.

Comments

5

The important thing to note here that even declaring integer array, the default should be defined as an empty object.

@Column("int", { array: true, default: {} })
ages: Number[];

Comments

4

For array, you can use simple-array as the type

1 Comment

Yes, there is an example stackoverflow.com/a/64301112/1074834
1

Typeorm now supports simple-array column type which automatically parses array values and saves them as a comma-separated string.

It will create a column of type text in postgres under the hood.

@Entity()
export class User {
    @PrimaryGeneratedColumn()
    id: number

    @Column("simple-array")
    names: string[]
}

Comments

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.