postgres 不支持 TypeORM 数组?

TypeORM array is not supported in postgres?

我有一列 kid_ages,它是 Integer[]。迁移时,出现以下错误:

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

我尝试将以下选项添加到我的专栏中:

type: 'array'

和:

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

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

文档说,它应该有效:

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

这是来自示例https://github.com/typeorm/typeorm/blob/master/test/functional/database-schema/column-types/postgres/entity/Post.ts

在您的代码中,数组 属性 不是数组。 你试过了吗kid_ages: string[];

如上所述,您可以使用下一个代码创建 Postgres 的数组列:

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

如果您随后需要找一些 5 岁的孩子,请使用:

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

对于希望在您的实体中处理数组或字符串的人,根据@thopaw 的回答,您可以使用以下代码:

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

我的 Postgres 检查数组包含的解决方案如下:

我定义的列如下:

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

"getFindQueryBuilder": 是为了节省代码获取共享SelectQueryBuilder的函数。

这里的重要部分是我在示例中显示的 where 子句。

这里需要注意的是,即使声明整数数组,默认也应该定义为空对象。

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

我尝试了上面提到的所有解决方案,但其中 none 行得通。终于在 TypeORM 存储库中的 Github 问题中找到了解决方案。

我引用解决方案:

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