gpt4 book ai didi

node.js - 使用 typeorm 在 postgres 中搜索数组中的项目

转载 作者:行者123 更新时间:2023-12-04 09:29:30 26 4
gpt4 key购买 nike

数据库 : postgres
ORM : 打字机
框架 : express.js
我有一个表,其中一个字段名为 projects是一个字符串数组。类型设置为 "varchar"在迁移和 de 装饰器设置为 "simple-array" .
如果我收到查询 ?project=name_of_the_project 在我的获取 route 它应该尝试在简单数组中找到项目。
对于搜索,我的获取路线是这样的:

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存储以逗号分隔的字符串。我认为这意味着它是一个字符串,单词以逗号分隔。在这种情况下,有没有办法在项目字段中找到哪一行有字符串?
链接 - https://gitee.com/mirrors/TypeORM/blob/master/docs/entities.md#column-types-for-postgres
编辑 02
字段projects存放的是学生们在做的项目,所以数据库返回这个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 })

关于node.js - 使用 typeorm 在 postgres 中搜索数组中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62904873/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com