zenstackhq / zenstack

Fullstack TypeScript toolkit that enhances Prisma ORM with flexible Authorization layer for RBAC/ABAC/PBAC/ReBAC, offering auto-generated type-safe APIs and frontend hooks.
https://zenstack.dev
MIT License
2.05k stars 87 forks source link

Primary key cannot be explicitly set in enhanced `create` when using extended models #1518

Closed KilianSL closed 3 months ago

KilianSL commented 3 months ago

Description The id field inherited from the parent model cannot be explicitly set when the enhanced create method is called on a "concrete" model. In my project, I have a model Activity, which declares the field id String @id @db.Uuid @default(uuid()), alongside some other shared fields. This model is then extended into a number of other models (i.e. VideoActivity). When calling the enhanced create action against one of the child models, if the id field is specified as part of the create, the action fails with error Unknown argument 'id'.

This bug can be worked around by not specifying the id field in the create call, and instead relying on the @default characteristic to generate a uuid for the entity, however this is not desirable in all cases.

Expected Behaviour Inlcuding a valid UUID in the create request should insert a row into the database with the provided UUID, overriding the default value.

Screenshots Minimal code snippets to reproduce the bug:

schema.zmodel

enum ActivityType {
  TaskActivity
}
model Activity {
  id        String       @id @db.Uuid @default(uuid())
  title     String
  type      ActivityType
  @@delegate(type)
  @@allow('all', true)
}
model TaskActivity extends Activity {
  description String
  @@map("task_activity")
  @@allow('all', true)
}

seed.ts

const client = new PrismaClient();
const prisma = enhance(client);
await prisma.taskActivity.create({
      data: {
        id: '00000000-0000-0000-0000-111111111111',
        title: 'Test Activity',
        description: 'Description of task',
      },
    });

Environment:

ymc9 commented 3 months ago

Fixed in v2.2.4