简体   繁体   English

NestJS / Prisma:键入'{ id:string; }' 不可分配给类型 'CustomersWhereUniqueInput'

[英]NestJS / Prisma : Type '{ id: string; }' is not assignable to type 'CustomersWhereUniqueInput'

Today I'm working on a new project with NestJS, I'm pretty new to NestJS and in particular to Prisma.今天我正在使用 NestJS 开发一个新项目,我对 NestJS 很陌生,尤其是 Prisma。 I followed this tutorial on the prisma documentation and tried to adapt it to my project.我遵循了有关 prisma 文档的 本教程,并尝试将其适应我的项目。

As the error says, the problem occured inside the findOne function.正如错误所说,问题发生在findOne function 内部。

I think it's my gestion of types.我认为这是我的类型。

I have read a lot of things but nothing seems to correspond to the error I'm getting.我已经阅读了很多东西,但似乎没有什么与我遇到的错误相对应。

Here is my schema.prisma:这是我的 schema.prisma:

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model Customers {
  Id    Int     @default(autoincrement()) @id
  Firstname String  
  Lastname  String
  Username String @unique
  Password String
  accounts     Bank_account[] 
}

model Admin {
  Id    Int     @default(autoincrement()) @id
  Username    String
  Password    String
  accounts    Bank_account[]
}

model Bank_account {
  Id      Int     @default(autoincrement()) @id
  Balance     Int
  RIB     String?
  owner     Customers  @relation(fields: [owner_id], references: [Id])
  owner_id    Int
  admin   Admin     @relation(fields: [admin_id], references: [Id])
  admin_id    Int
  log_id    Int  
  
}

model Logs {
  Id    Int   @default(autoincrement()) @id
  Date   String?
  Type   String?
  State   Boolean?
  Bank_account    Int    
}

My customers.services.ts我的 customers.services.ts

import { CreateCustomerDto } from './dto/create-customer.dto';
import { UpdateCustomerDto } from './dto/update-customer.dto';
import { Get, Injectable, Param } from '@nestjs/common';
import { PrismaService } from 'src/prisma/prisma.service';
import { isNumberObject } from 'util/types';
import { Customer } from './entities/customer.entity.js';

@Injectable()export class CustomersService {  constructor(private prisma: PrismaService) {}

  create(createCustomerDto: CreateCustomerDto) {
    return 'This action adds a new customer';
  }

  findAll() {        
    return this.prisma.customers.findMany();  
    }

  @Get(':id')
  findOne(@Param('id') id: string) {  
    return this.prisma.customers.findUnique({ where: { id }});
  }

Everything seems alright with my controllers and modules.我的控制器和模块似乎一切正常。

I already tried to re-generate prisma.schema, I tried to add some useless values to my database, nothing seems to change.我已经尝试重新生成 prisma.schema,我尝试向我的数据库添加一些无用的值,但似乎没有任何改变。

I tried to change the ìd for Id but nest said Cannot find name 'Id'. Did you mean 'id'?我试图更改Idìd但 nest 说Cannot find name 'Id'. Did you mean 'id'? Cannot find name 'Id'. Did you mean 'id'?

Type '{ id: string;输入 '{ id: string; }' is not assignable to type 'CustomersWhereUniqueInput' }' 不可分配给类型 'CustomersWhereUniqueInput'

Look at your column type for id :查看id的列类型:

Id    Int

when the primary key is a Int type, Prisma expects numeric ids in your queries.当主键是Int类型时,Prisma 期望在您的查询中使用数字 ID。

So convert that string to an integer before you pass it to Prisma.因此,在将该字符串传递给 Prisma 之前将该字符串转换为 integer。

  @Get(':id')
  findOne(@Param('id') id: string) {  
    return this.prisma.customers.findUnique({ where: { id: parseInt(id) }});
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM