Search code examples
pythonpython-3.xprisma

Prisma asking for required ID which is marked as default(autoincrement() in schema


I'm using the Python Prisma client. Below is my schema.prisma which should make it so that whenever I create something in the serververification table, it will have an autoincremented ID on that inserted data.

The database is a MySQL database.

model serververification {
  id               Int     @id @default(autoincrement())
  guildId          String  @db.VarChar(20)
  enabled          Boolean @db.Bit(1)
  panelMessageId   String? @db.VarChar(20)
  panelChannelId   String? @db.VarChar(20)
  logChannelId     String? @db.VarChar(20)
  unverifiedRoleId String? @db.VarChar(20)
  verifiedRoleIds  String? @db.LongText

  @@index([guildId], map: "guildId_fkey")
}

However, this does not seem to be the case. If I don't personally specify an ID I get an error stating that A value is required but not set. I'm not entirely sure what I've gone wrong. The schema is up to date with the database, I have ran all necessary commands

yarn prisma generate
yarn prisma db push
yarn prisma migrate dev
yarn prisma db pull

Code where I create the new data to insert into the database, notice how I do not include a id key value, because the schema specifies that it shouldn't require me to, since the field is an auto incremented integer ID.

        await self.client.prisma.serververification.create(
            data={
                "guildId": str(interaction.guild_id),
                "enabled": True,
                "panelMessageId": str(panel_message.id),
                "panelChannelId": str(panel_channel_id.id),
                "logChannelId": None,
                "unverifiedRoleIds": str(unverified_role_id.id),
            }
        )

Python prisma version: v0.6.6 other version information:

{
  "dependencies": {
    "@prisma/client": "^4.1.1",
    "prisma": "^4.1.1"
  }
}

error:

prisma.errors.FieldNotFoundError: Failed to validate the query: `Unable to match input value to any allowed input type for the field. Parse errors: [Query parsing/validation error at `Mutation.createOneserververification.data.serververificationCreateInput.unverifiedRoleIds`: Field does not exist on enclosing type., Query parsing/validation error at `Mutation.createOneserververification.data.serververificationUncheckedCreateInput.unverifiedRoleIds`: Field does not exist on enclosing type.]` at `Mutation.createOneserververification.data`

HeidiSQL database structure


Solution

  • In your schema file, you defined a field called unverifiedRoleId. In your client when assigning a value to that field, you added an extra character s to the field name. Instead of unverifiedRoleId, you are using unverifiedRoleIds. Note the plural form here. To fix this issue, ensure both the field name in the schema file matches with the field name in the client.

    schema.prisma file

    model serververification {
      id               Int     @id @default(autoincrement())
      guildId          String  @client.VarChar(20)
      enabled          Boolean @client.Bit(1)
      panelMessageId   String? @client.VarChar(20)
      panelChannelId   String? @client.VarChar(20)
      logChannelId     String? @client.VarChar(20)
      unverifiedRoleId String? @client.VarChar(20)
      verifiedRoleIds  String? @client.LongText
    
      @@index([guildId], map: "guildId_fkey")
    }
    

    main.py file

    serververification = await db.serververification.create(
            data = {
                    "guildId":"abcdefgh",
                    "enabled": True,
                    "panelMessageId": "12345678",
                    "panelChannelId": "qwerty",
                    "logChannelId": None,
                    "unverifiedRoleId": "poiuyt",
            }
        )
    
        print(f'created serververification: {serververification.json(indent=2, sort_keys=True)}')
    
        found = await db.serververification.find_unique(where={'id': serververification.id})
        assert found is not None
        print(f'found serververification: {found.json(indent=2, sort_keys=True)}')
    

    output

    created serververification: {
      "enabled": true,
      "guildId": "abcdefgh",
      "id": 4,
      "logChannelId": null,
      "panelChannelId": "qwerty",
      "panelMessageId": "12345678",
      "unverifiedRoleId": "poiuyt",
      "verifiedRoleIds": null
    }
    found serververification: {
      "enabled": true,
      "guildId": "abcdefgh",
      "id": 4,
      "logChannelId": null,
      "panelChannelId": "qwerty",
      "panelMessageId": "12345678",
      "unverifiedRoleId": "poiuyt",
      "verifiedRoleIds": null
    }