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

find by composite primary keys API is super weird #489

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

find by composite primary keys API is super weird #489

matthewmueller opened this issue Feb 13, 2020 · 2 comments
Labels
kind/feature A request for a new feature.
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');
model variables {
  email String
  key   String
  name  String
  value String

  @@id([name, key])
}
client.variables.findOne({ where: { variables_name_key_key: { key: 'b', name: 'a' } } })
  • Why is the table name in there?
  • _key seems unnecessary

I think it's just taking the default postgres primary key name.

Proposal

Ideally the API looks like this:

client.variables.findOne({ where: { key: 'b', name: 'a' } })
@janpio janpio changed the title find by composite primary keys API is super wierd find by composite primary keys API is super weird Feb 13, 2020
@pantharshit00 pantharshit00 added the kind/improvement An improvement to existing feature and code. label Feb 14, 2020
@janpio janpio added kind/feature A request for a new feature. and removed kind/improvement An improvement to existing feature and code. labels Mar 15, 2020
@janpio
Copy link
Member

janpio commented Mar 19, 2020

The original problem with variables_name_key_key seems to have been resolved:

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?

Your proposal is still not valid though, please create a specs issue or PR for that one so we can keep this here closed. Thanks.

@janpio janpio closed this as completed Mar 19, 2020
@janpio janpio added this to the Preview 25 milestone Mar 19, 2020
@matthewmueller
Copy link
Contributor Author

Looks good 👍

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind/feature A request for a new feature.
Projects
None yet
Development

No branches or pull requests

3 participants