Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factory creation fails for models with composite types #424

Open
kitoko552 opened this issue Dec 25, 2024 · 0 comments
Open

Factory creation fails for models with composite types #424

kitoko552 opened this issue Dec 25, 2024 · 0 comments

Comments

@kitoko552
Copy link

kitoko552 commented Dec 25, 2024

Describe the bug
When attempting to create a model that includes a composite type field using factory, the creation fails with a validation error. The error indicates that the composite type field is "missing" even though it should be automatically handled by the factory.

Example schema:

model Speaker {
  id        String    @id @default(auto()) @map("_id") @db.ObjectId
  name      String
  thumbnail ImageData
  @@map("speakers")
}
type ImageData {
  uri    String
  width  Int
  height Int
}

The factory creation throws the following error:

PrismaClientValidationError:
Invalid `getClient<PrismaClient>().speaker.create()` invocation in
/{myprojectpath}/generated/fabbrica/index.ts:7372:73

  7369 const [transientFields] = destructure(defaultTransientFieldValues, inputData);
  7370 const data = await build(inputData).then(screen);
  7371 await handleBeforeCreate(data, transientFields);
→ 7372 const createdData = await getClient<PrismaClient>().speaker.create({
         data: {
           name: "name field 0",
       +   thumbnail: ImageDataCreateEnvelopeInput | ImageDataCreateInput
         }
       })

Argument `thumbnail` is missing.

This appears to be an issue with how fabbrica handles composite types in its factory generation or execution process.

To Reproduce

  1. Set up a new Prisma project with MongoDB as the database provider
  2. Add the following schema to your schema.prisma:
datasource db {
  provider = "mongodb"
  url      = env("MONGODB_URL")
}
model Speaker {
  id        String    @id @default(auto()) @map("_id") @db.ObjectId
  name      String
  thumbnail ImageData
  @@map("speakers")
}
type ImageData {
  uri    String
  width  Int
  height Int
}
  1. Generate the factories by prisma generate
  2. Try to create a Speaker using the generated factory:
import { defineSpeakerFactory } from './path-to-generated-factories'
const speakerFactory = defineSpeakerFactory()
await speakerFactory.create()
  1. The creation will fail with the error

Expected behavior
The factory should automatically generate valid test data for all fields, including the composite type ImageData.

Environment (please complete the following information):

  • Database kind: MongoDB
  • Node.js version: 22.11.0
  • @prisma/client version: 5.22.0
  • TypeScript version: 5.6.3

Your Schema
See To Reproduce section

Additional context
Upon examining the generated factory code, I found that the modelFieldDefinitions for the ImageData composite type has an empty relationName:

const modelFieldDefinitions: ModelWithFields[] = [{
    name: "Speaker",
    fields: [{
            name: "thumbnail",
            type: "ImageData",
            relationName: ""
        }]
  }];

This empty relationName for the composite type field might be contributing to the validation error, as it could affect how fabbrica handles the field during factory creation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant