keystone icon indicating copy to clipboard operation
keystone copied to clipboard

Integer field defaultValue {kind: "autoincrement"} triggers missing value validation

Open simonsagstetter opened this issue 10 months ago • 2 comments

Configure a list with an integer autoincrement field

import { list } from "@keystone-6/core";
import { integer } from "@keystone-6/core/fields";

list({
 fields: {
  no: integer({
        defaultValue: { kind: "autoincrement" },
        db: { isNullable: false }, // if this is not set I get a prisma error
    }),
 }
});

Prisma Error when db.isNullable is not set to false Error loading your Keystone config Error: Request.no specifies defaultValue: { kind: 'autoincrement' } but doesn't specify db.isNullable: false. Having nullable autoincrements on Prisma currently incorrectly creates a non-nullable column so it is not allowed. https://github.com/prisma/prisma/issues/8663

I also tried validation: { isRequired: false}

When you try to create a new record for this list it will return an validation error for a missing value on that field: You provided invalid data for this operation. - Request.no: missing value

Image

I setup the integer field like described in the docs: defaultValue (default: undefined): Can be either an integer value or { kind: 'autoincrement' }. This value will be used for the field when creating items if no explicit value is set. { kind: 'autoincrement' } is only supported on PostgreSQL.

I should not need to put in a value for that field on any db operation because it is configured to auto-increment. When I put in a value into the integer field this value will be saved. What am I missing or is this a bug?

My db config

...
db: {
            provider: "postgresql",
            url: `postgres://${process.env.DB_USER}:${process.env.DB_PASS}@localhost:5432/keystone`,
            enableLogging: false,
            idField: { kind: "uuid" }, // is it because of this??!
            shadowDatabaseUrl: `postgres://${process.env.DB_USER}:${process.env.DB_PASS}@localhost:5432/shadowdb`,
},
...
node version v20.18.1
brave browser v1.74.50
postgresql v17.2
keystone-6/core v6.3.1

simonsagstetter avatar Jan 28 '25 17:01 simonsagstetter

@simonsagstetter, please try:

no: integer({
  defaultValue: { kind: "autoincrement" },
  db: { isNullable: false }, // Required for autoincrement
  validation: { isRequired: false } // Explicitly set to false
})

Maybe it's worth also considering for the project:

  • Improve documentation to clearly explain the required configuration for autoincrement fields
  • Consider automatically setting validation.isRequired: false when defaultValue: { kind: 'autoincrement' } is used
  • Add a more helpful error message that suggests the complete solution
  • Monitor the Prisma issue (#8663) for resolution

lushkovsky-s avatar Mar 03 '25 12:03 lushkovsky-s

@lushkovsky-s thanks your your reply.

I already tried to set validation.isRequired to false as I stated in my issue. It doesn't affect the error outcome in any way...

simonsagstetter avatar Mar 03 '25 15:03 simonsagstetter