Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

495
Views
NestJS Postgres Prisma: el tipo de error 'cadena' no se puede asignar al tipo de parámetro 'TemplateStringsArray | Sql'

Estoy tratando de construir un servicio NestJS Monorepo utilizando la arquitectura de microservicios con PostgreSQL como base de datos, Prisma como ORM y TypeScript como idioma principal. Pero sigo recibiendo el siguiente error cuando intento ejecutar una consulta de Postgres.

src/infrastructure/persistence/work.repository.postgres.ts:188:7 - error TS2345: el argumento de tipo 'cadena' no se puede asignar al parámetro de tipo 'TemplateStringsArray | Sql'.

Revisé los tipos de datos y parecen ser compatibles.

Por favor ayúdame a arreglar esto.

Pila: TypeScript, PostgreSQL, Node.JS, Express y NestJS.

¡Gracias por adelantado!

 async findWriterNumber(writerAddress: string): Promise<number> { const maxNumber = await this.prismaService.$queryRaw<{ max: number; }>( `SELECT coalesce(max('writerNumber') + 1, 0) as max FROM "Work" LEFT OUTER JOIN "WriterID" ON "Work"."id" = "WorkID"."workId" AND "WorkID"."address" = '${writerAddress}'`, ); return maxNumber[0].max; }
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Encontré una solución simple:

Quite ( ) alrededor de la instrucción sql.

 async findWriterNumber(writerAddress: string): Promise<number> { const maxNumber = await this.prismaService.$queryRaw<{ max: number; }> `SELECT coalesce(max('writerNumber') + 1, 0) as max FROM "Work" LEFT OUTER JOIN "WriterID" ON "Work"."id" = "WorkID"."workId" AND "WorkID"."address" = '${writerAddress}'`; return maxNumber[0].max; }

Una declaración preparada traducirá la consulta a "seleccione coalesce... from work where... address=$1" y envíe $1 como parámetro, por lo que no necesita envolverlo entre comillas.

over 4 years ago · Santiago Trujillo Report

0

Otra solución es usar el ayudante Prisma.sql :

 const maxNumber = await this.prismaService.$queryRaw<{ max: number; }>( Prisma.sql`SELECT coalesce(max('writerNumber') + 1, 0) as max FROM "Work" LEFT OUTER JOIN "WriterID" ON "Work"."id" = "WorkID"."workId" AND "WorkID"."address" = '${writerAddress}'`, ); return maxNumber[0].max;
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!