使用 typeorm 在 postgres 的数组中搜索项目
Search item in array at postgres using typeorm
数据库:postgres
ORM: Typeorm
框架:express.js
我有一个 Table,其中一个名为 projects
的字段是一个字符串数组。类型在迁移中设置为 "varchar"
并且de decorator设置为 "simple-array"
.
如果我收到查询 ?project=name_of_the_project
在我的获取路径中,它应该尝试在简单数组中找到项目。
对于搜索,我的获取路线是这样的:
studentsRouter.get("/", async (request, response) => {
const { project } = request.query;
const studentRepository = getCustomRepository(StudentRepository);
const students = project
? await studentRepository
.createQueryBuilder("students")
.where(":project = ANY (students.projects)", { project: project })
.getMany()
: await studentRepository.find();
// const students = await studentRepository.find();
return response.json(students);
});
问题是我收到一条错误消息,指出右侧应该是一个数组。
(node:38971) UnhandledPromiseRejectionWarning: QueryFailedError: op ANY/ALL (array) requires array on right side
at new QueryFailedError (/Users/Wblech/Desktop/42_vaga/src/error/QueryFailedError.ts:9:9)
at Query.callback (/Users/Wblech/Desktop/42_vaga/src/driver/postgres/PostgresQueryRunner.ts:178:30)
at Query.handleError (/Users/Wblech/Desktop/42_vaga/node_modules/pg/lib/query.js:146:19)
at Connection.connectedErrorMessageHandler (/Users/Wblech/Desktop/42_vaga/node_modules/pg/lib/client.js:233:17)
at Connection.emit (events.js:200:13)
at /Users/Wblech/Desktop/42_vaga/node_modules/pg/lib/connection.js:109:10
at Parser.parse (/Users/Wblech/Desktop/42_vaga/node_modules/pg-protocol/src/parser.ts:102:9)
at Socket.<anonymous> (/Users/Wblech/Desktop/42_vaga/node_modules/pg-protocol/src/index.ts:7:48)
at Socket.emit (events.js:200:13)
at addChunk (_stream_readable.js:294:12)
(node:38971) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:38971) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
该字段必须是字符串数组,我不能使用外键。
请在下面找到与此问题相关的迁移和模型:
迁移:
import { MigrationInterface, QueryRunner, Table } from "typeorm";
export class CreateStudents1594744103410 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.createTable(
new Table({
name: "students",
columns: [
{
name: "id",
type: "uuid",
isPrimary: true,
generationStrategy: "uuid",
default: "uuid_generate_v4()",
},
{
name: "name",
type: "varchar",
},
{
name: "intra_id",
type: "varchar",
isUnique: true,
},
{
name: "projects",
type: "varchar",
isNullable: true,
},
],
})
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropTable("students");
}
}
型号:
import { Entity, Column, PrimaryGeneratedColumn } from "typeorm";
@Entity("students")
class Student {
@PrimaryGeneratedColumn("uuid")
id: string;
@Column()
name: string;
@Column()
intra_id: string;
@Column("simple-array")
projects: string[];
}
export default Student;
编辑 - 01
在文档中我发现 simple-array
存储由逗号分隔的字符串。我认为这意味着它是一个由逗号分隔的单词的字符串。在这种情况下,有没有办法找到项目字段中哪一行有字符串?
Link - https://gitee.com/mirrors/TypeORM/blob/master/docs/entities.md#column-types-for-postgres
编辑 02
实地项目存储学生正在做的项目,所以数据库returns这个json:
{
"id": "e586d1d8-ec03-4d29-a823-375068de23aa",
"name": "First Lastname",
"intra_id": "flastname",
"projects": [
"42cursus_libft",
"42cursus_get-next-line",
"42cursus_ft-printf"
]
},
根据问题的评论和更新,@WincentyBertoniLech 确定 ORM 将 projects
数组作为逗号分隔的文本值存储在 students.projects
列中。
我们可以使用 string_to_array()
将其转换为正确的 where
标准:
.where(":project = ANY ( string_to_array(students.projects, ','))", { project: project })
数据库:postgres
ORM: Typeorm
框架:express.js
我有一个 Table,其中一个名为 projects
的字段是一个字符串数组。类型在迁移中设置为 "varchar"
并且de decorator设置为 "simple-array"
.
如果我收到查询 ?project=name_of_the_project
在我的获取路径中,它应该尝试在简单数组中找到项目。
对于搜索,我的获取路线是这样的:
studentsRouter.get("/", async (request, response) => {
const { project } = request.query;
const studentRepository = getCustomRepository(StudentRepository);
const students = project
? await studentRepository
.createQueryBuilder("students")
.where(":project = ANY (students.projects)", { project: project })
.getMany()
: await studentRepository.find();
// const students = await studentRepository.find();
return response.json(students);
});
问题是我收到一条错误消息,指出右侧应该是一个数组。
(node:38971) UnhandledPromiseRejectionWarning: QueryFailedError: op ANY/ALL (array) requires array on right side
at new QueryFailedError (/Users/Wblech/Desktop/42_vaga/src/error/QueryFailedError.ts:9:9)
at Query.callback (/Users/Wblech/Desktop/42_vaga/src/driver/postgres/PostgresQueryRunner.ts:178:30)
at Query.handleError (/Users/Wblech/Desktop/42_vaga/node_modules/pg/lib/query.js:146:19)
at Connection.connectedErrorMessageHandler (/Users/Wblech/Desktop/42_vaga/node_modules/pg/lib/client.js:233:17)
at Connection.emit (events.js:200:13)
at /Users/Wblech/Desktop/42_vaga/node_modules/pg/lib/connection.js:109:10
at Parser.parse (/Users/Wblech/Desktop/42_vaga/node_modules/pg-protocol/src/parser.ts:102:9)
at Socket.<anonymous> (/Users/Wblech/Desktop/42_vaga/node_modules/pg-protocol/src/index.ts:7:48)
at Socket.emit (events.js:200:13)
at addChunk (_stream_readable.js:294:12)
(node:38971) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:38971) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
该字段必须是字符串数组,我不能使用外键。
请在下面找到与此问题相关的迁移和模型:
迁移:
import { MigrationInterface, QueryRunner, Table } from "typeorm";
export class CreateStudents1594744103410 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.createTable(
new Table({
name: "students",
columns: [
{
name: "id",
type: "uuid",
isPrimary: true,
generationStrategy: "uuid",
default: "uuid_generate_v4()",
},
{
name: "name",
type: "varchar",
},
{
name: "intra_id",
type: "varchar",
isUnique: true,
},
{
name: "projects",
type: "varchar",
isNullable: true,
},
],
})
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropTable("students");
}
}
型号:
import { Entity, Column, PrimaryGeneratedColumn } from "typeorm";
@Entity("students")
class Student {
@PrimaryGeneratedColumn("uuid")
id: string;
@Column()
name: string;
@Column()
intra_id: string;
@Column("simple-array")
projects: string[];
}
export default Student;
编辑 - 01
在文档中我发现 simple-array
存储由逗号分隔的字符串。我认为这意味着它是一个由逗号分隔的单词的字符串。在这种情况下,有没有办法找到项目字段中哪一行有字符串?
Link - https://gitee.com/mirrors/TypeORM/blob/master/docs/entities.md#column-types-for-postgres
编辑 02
实地项目存储学生正在做的项目,所以数据库returns这个json:
{
"id": "e586d1d8-ec03-4d29-a823-375068de23aa",
"name": "First Lastname",
"intra_id": "flastname",
"projects": [
"42cursus_libft",
"42cursus_get-next-line",
"42cursus_ft-printf"
]
},
根据问题的评论和更新,@WincentyBertoniLech 确定 ORM 将 projects
数组作为逗号分隔的文本值存储在 students.projects
列中。
我们可以使用 string_to_array()
将其转换为正确的 where
标准:
.where(":project = ANY ( string_to_array(students.projects, ','))", { project: project })