Prisma adapter fails with create query on relationship field to List item with non-number id
See original GitHub issueBug report
Describe the bug
Prisma adapter fails with create query on relationship field to List item with non-number id
To Reproduce
- Modify the
examples-next/todo
demo via changing id field type of “Todo” list to text and commenting thetrackingFields
(to remove unnecessary fields):
Todo: list({
ui: {
listView: {
initialColumns: ['label', 'isComplete', 'createdAt', 'createdBy', 'updatedAt', 'updatedBy'],
},
},
fields: {
label: text({ isRequired: true }),
isComplete: checkbox(),
assignedTo: relationship({ ref: 'User.tasks' }),
finishBy: timestamp(),
},
idField: text({
isUnique: true,
isIndexed: true,
access: {
create: true,
},
ui: {
createView: { fieldMode: 'edit' },
itemView: { fieldMode: 'read' },
},
}),
}),
- Create new Todo item via Admin UI (with patch from #4826, or directly via code without patch) with id
task_23
- it will be created well. - Try to create new user “Alice” without relationship to task - created successfully.
- Try to create new user “Bob” and select the relationship to some task - you will got the error:
Invalid `prisma.user.create()` invocation: { data: { name: 'Bob', tasks: { connect: [ { id: NaN ~~~ } ] } }, include: { tasks: { select: { id: true } } } } Argument id: Got invalid value NaN on prisma.createOneUser. Provided Float, expected String.
Here is GraphQL query that sent to server:
mutation ($data: UserCreateInput!) {
item: createUser(data: $data) {
id
label: name
__typename
}
}
and query:
{
"data": {
"name": "Bob",
"tasks": {
"connect": [
{
"id": "task_23"
}
]
}
}
}
Expected behaviour
The User item must be created with linked Todo item without errors. With knex
and mongoose
adapters this works well.
Seems the problem is in this part of code: https://github.com/keystonejs/keystone/blob/f029a3bd6f54deb7b336427fa33e8bb237e3b5eb/packages/adapter-prisma/lib/adapter-prisma.js#L320
async _create(_data) {
return this.model.create({
data: mapKeys(_data, (value, path) =>
this.fieldAdaptersByPath[path] && this.fieldAdaptersByPath[path].isRelationship
? {
connect: Array.isArray(value)
* ? value.map(x => ({ id: Number(x) }))
* : { id: Number(value) },
}
: this.fieldAdaptersByPath[path] && this.fieldAdaptersByPath[path].gqlToPrisma
? this.fieldAdaptersByPath[path].gqlToPrisma(value)
: value
),
include: this._include(),
});
}
In this function Keystone force the conversion of id
value to Number always. But we must lookup the target field type and convert to number only if have numeric field type on remote side.
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (12 by maintainers)
Top Results From Across the Web
Troubleshooting relations - Prisma
Solution. If you rename relation fields in an implicit many-to-many self-relations, make sure that you maintain the alphabetic order of the fields ......
Read more >Relations (Reference) - Prisma
A relation is a connection between two models in the Prisma schema. This page explains how you can define one-to-one, one-to-many and many-to-many...
Read more >Querying models based on their relations existence - Prisma
This query returns all the users that created at least one post. This article showed how you can query for entities based on...
Read more >Zod | Documentation
Zod is a TypeScript-first schema declaration and validation library. I'm using the term "schema" to broadly refer to any data type, from a...
Read more >Untitled
Make up kit cartoon, Atego 1725 42a, Tabs 4 sale, Counter strike source zombie 2014, Ultima vez whatsapp que significa. Receptor based pharmacophore,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
At the moment the
autoIncrement
field type is the only one which will work as anidField
. The new changes will definitely include auuid
field type, and we’re also investigating adding support for other index field types as needed.I’m going to close this issue for now, as we don’t currently support non-autoincrement idFields. We will revisit this as a feature once the changes in #5635 have been completed.