question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prisma adapter fails with create query on relationship field to List item with non-number id

See original GitHub issue

Bug report

Describe the bug

Prisma adapter fails with create query on relationship field to List item with non-number id

To Reproduce

  1. Modify the examples-next/todo demo via changing id field type of “Todo” list to text and commenting the trackingFields (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' },
      },
    }),
  }),
  1. 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.
  2. Try to create new user “Alice” without relationship to task - created successfully.
  3. 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:closed
  • Created 3 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
timlesliecommented, May 7, 2021

At the moment the autoIncrement field type is the only one which will work as an idField. The new changes will definitely include a uuid field type, and we’re also investigating adding support for other index field types as needed.

1reaction
timlesliecommented, May 6, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found