Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The type of where part of delete or update function is wrong when using @@unique #11284

Closed
rostamiani opened this issue Jan 20, 2022 · 4 comments
Labels
kind/bug A reported bug.

Comments

@rostamiani
Copy link

Bug description

I have a model containing @@unique index. When I want to add where section to delete function it's type is wrong.

How to reproduce

Generate this model:

model User {
  id        Int       @id @default(autoincrement())
  email     String    @unique
  password  String
  createdAt DateTime  @default(now())
  updatedAt DateTime  @updatedAt
  remarks   String?
  Channels  Channel[]
}

model Channel {
  id     Int    @id @default(autoincrement())
  name   String
  userId Int
  user   User   @relation(fields: [userId], references: [id])

  @@unique([userId, name])
  @@index([name])
}

It generates this class:

  export type ChannelDeleteArgs = {
    select?: ChannelSelect | null
    include?: ChannelInclude | null
    where: ChannelWhereUniqueInput
  }

  export type ChannelWhereUniqueInput = {
    id?: number
    userId_name?: ChannelUserIdNameCompoundUniqueInput
  }

The where part is different from find and create commands and does not have channel fields. Now I see this error:

'userId' does not exist in type 'ChannelWhereUniqueInput'

Expected behavior

I want to delete a channel with this comand:

await this.prisma.channel.delete({ where: { id: 1, userId:1 } });

Prisma information

Environment & setup

  • OS: Ubuntu 21.10
  • Database: PostgreSQL
  • Node.js version: v16.13.1

Prisma Version

3.8.1
@rostamiani rostamiani added the kind/bug A reported bug. label Jan 20, 2022
@unsady
Copy link

unsady commented Jan 20, 2022

Hello! The correct query should be:

await this.prisma.channel.delete({ where: { userId_name: { userId: 1, name: 'John' } });

You can use editor suggestions (CTRL+space for vscode) to see available options.

If you need to delete rows by channel id and userId then you should create a unique index @@unique([userId, id]). Where condition should be:

await this.prisma.channel.delete({ where: { userId_id: { userId: 1, id: 1 } });

You can also delete all rows

await this.prisma.channel.deleteMany({ where: { id: 1, userId:1 } });

@rostamiani
Copy link
Author

I want to delete just one single row and only if it's belongs to a specific user. But I don't know the name
Why is it this complicated?
Why I cannot delete or update with my needed columns like this?:

this.prisma.channel.update({ where: { id: request.id, userId }, data:{name:'new Name'} });
this.prisma.channel.u({ where: { id: request.id, userId } });

@rostamiani rostamiani changed the title The type of where part of delete function is wrong when using @@unique The type of where part of delete or update function is wrong when using @@unique Jan 21, 2022
@ruohola
Copy link

ruohola commented Jan 24, 2022

I want to delete just one single row and only if it's belongs to a specific user. But I don't know the name Why is it this complicated? Why I cannot delete or update with my needed columns like this?:

this.prisma.channel.update({ where: { id: request.id, userId }, data:{name:'new Name'} });
this.prisma.channel.u({ where: { id: request.id, userId } });

This is duplicate of #7290

@matthewmueller
Copy link
Contributor

matthewmueller commented Jan 28, 2022

Thanks @ruohola for spotting that, closing as a duplicate of #7290

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug A reported bug.
Projects
None yet
Development

No branches or pull requests

4 participants