Skip to content
This repository has been archived by the owner on Jan 14, 2021. It is now read-only.

Can't findOne, update, delete on composite primary keys #488

Closed
matthewmueller opened this issue Feb 13, 2020 · 5 comments
Closed

Can't findOne, update, delete on composite primary keys #488

matthewmueller opened this issue Feb 13, 2020 · 5 comments
Assignees
Labels
bug/0-needs-info More information is needed for reproduction. kind/bug A reported bug. topic: AUMFIDARR
Milestone

Comments

@matthewmueller
Copy link
Contributor

matthewmueller commented Feb 13, 2020

create table variables (
  name text not null,
  key text not null,
  value text not null,
  email text not null,
  primary key(name, key)
);
insert into variables (name, key, value, email) values ('a', 'b', 'c', 'd');
// typescript error
client.variables.findOne({ where: { variables_name_key_key: { key: 'b', name: 'a' } } })
client.variables.update({ where: { variables_name_key_key: { key: 'b', name: 'a' } } })
client.variables.delete({ where: { variables_name_key_key: { key: 'b', name: 'a' } } })

Related: #489

@matthewmueller matthewmueller added the process/candidate Candidate for next Milestone. label Feb 13, 2020
@pantharshit00 pantharshit00 added kind/feature A request for a new feature. bug/2-confirmed We have confirmed that this is a bug. kind/bug A reported bug. and removed kind/feature A request for a new feature. labels Feb 14, 2020
@divyenduz divyenduz added this to the Preview 23 milestone Feb 14, 2020
@janpio
Copy link
Member

janpio commented Feb 14, 2020

Note: Reproduce again.

@pantharshit00
Copy link
Contributor

I can't reproduce this with prisma2@2.0.0-alpha.797, binary version: f608db3b9e4f50f9a384356e532357b0a525969a. Initially when I marked this as a confirmed bug I assumed that @matthewmueller referred to implementation that client will have @@id. Right now no such functionality is exposed via the client.

@pantharshit00 pantharshit00 added bug/0-needs-info More information is needed for reproduction. and removed bug/2-confirmed We have confirmed that this is a bug. labels Feb 18, 2020
@divyenduz divyenduz removed the process/candidate Candidate for next Milestone. label Feb 18, 2020
@janpio janpio modified the milestones: Preview 23, Preview 24 Mar 3, 2020
@divyenduz divyenduz modified the milestones: Preview 24 Old, Preview 25 Mar 13, 2020
@janpio
Copy link
Member

janpio commented Mar 19, 2020

Introspecting a Postgres DB that has the above SQL generates this schema:

model variables {
  email String
  key   String
  name  String
  value String

  @@id([name, key])
}

Afterwards the following index.ts (I commented in and out the individual lines to run them one by one) works:

import { PrismaClient } from '@prisma/client'

const client = new PrismaClient()

client.variables.create({ data: { name: 'a', key: 'b', email: 'mail@example.org', value: 'foo' } }).then(res => { console.log('create', res) })
client.variables.findOne({ where: { name_key: { key: 'b', name: 'a' } } }).then(res => { console.log('findOne', res) })
client.variables.update({ where: { name_key: { key: 'b', name: 'a' } }, data: { value: 'bar' } }).then(res => { console.log('update', res) })
client.variables.delete({ where: { name_key: { key: 'b', name: 'a' } } }).then(res => { console.log('delete', res) })

(prisma2@2.0.0-alpha.934)

Can you please confirm that this is the wanted behavior @matthewmueller?

@janpio janpio closed this as completed Mar 19, 2020
@dpankros
Copy link

dpankros commented Dec 1, 2020

Can I request that composite keys be documented in the find/update/delete sections of the docs (or even a separate section that gives a hint how it works)? I was stuck on them for days and didn't think they worked properly because the schema was showing my where options as including something like <key1>_<key2>which didn't make sense to me and nothing I tried to supply there worked.

Thanks to @janpio 's post, above, now I see how it works and I'm unstuck. Documentation of this point would have saved a tremendous amount of time.

@nikolasburk
Copy link
Member

Hey @dpankros 👋 yes, I just created an issue in the docs repo to persist this, our docs team will look into this soon! 🙏

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug/0-needs-info More information is needed for reproduction. kind/bug A reported bug. topic: AUMFIDARR
Projects
None yet
Development

No branches or pull requests

6 participants