Skip to content

[BUG] Typescript asks for delegation field when creating new polymorphic instance and providing id with a default of auto increment #1763

@Tsimopak

Description

@Tsimopak

Description and expected behavior
Typescript asks for delegation field when creating new polymorphic instance when providing id with a default of auto increment.

Screenshots
image

Steps to reproduce

  1. pnpm init
  2. pnpm i -D typescript
  3. npx tsc --init
  4. npx zenstack init
  5. Copy paste the following models to your schema:
model Post {
    id Int @id @default(autoincrement())
    name String

    type String
    @@delegate(type)

    // full access by author
    @@allow('all', true)
}

model ConcretePost extends Post {
    age Int
}
  1. Run npx zenstack generate && npx prisma migrate dev --name "update"
  2. Create index.ts file at root and copy paste the following content:
import { PrismaClient as Prisma } from "@prisma/client";
import { enhance } from "@zenstackhq/runtime";

const prisma = new Prisma()
const db = enhance(prisma, {}, {
  kinds: ['delegate']
})

async function main() {
  const newPost = await db.concretePost.create({
    data: {
      id: 5,
      name: 'a name',
      age: 20
    }
  })
  const posts = await db.post.findMany({})
  console.log('posts', posts)
}

main()

Now you will see that typescript shouts on data saying that type field is missing.

Btw if you execute this code (You can run on terminal npx tsx index.ts) it will work just fine.

Environment (please complete the following information):

  "devDependencies": {
    "prisma": "^5.20.0",
    "typescript": "^5.6.2",
    "zenstack": "2.6.2"
  },
  "dependencies": {
    "@prisma/client": "^5.20.0",
    "@zenstackhq/runtime": "2.6.2"
  }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions