Skip to content

Factory doesn't return a db object #162

Open
@adrijere

Description

@adrijere

Hi 👋
First of all, thank you very much for your work. After your answer, we decided to use your lib on our on production application and it saved us a lot of time!

Describe the bug
As mentioned in the documentation, I create a UserProfile which has a relation with the table User. When I use the factory to create a UserProfile, the return object is not similar to one from the DB so I can't call my relation like userProfile.user.

To Reproduce
Create a relation like this:

export const UserProfileFactory = defineUserProfileFactory({
  defaultData: () => ({
    id: chance.guid(), // chance is a lib similar to faker
    firstName: chance.first(),
    user: UserFactory,
  }),
});

In my test, I do : const recipientProfile = await UserProfileFactory.create();.
My recipientProfile doesn't have a user property but a userId which means if I want to do recipientProfile.user I can't and I have to do a prisma call to get my User db object.

Expected behavior
My factory returns a DB object and so I can easily use my relation through my new created object.

Your Schema

model User {
  id                  String              @id @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
  createdAt           DateTime            @default(now())
  updatedAt           DateTime            @default(now()) @updatedAt
  email               String              @unique
  userProfiles        UserProfile[]
  }

model UserProfile {
  id                  String       @id @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
  createdAt           DateTime     @default(now())
  updatedAt           DateTime     @default(now()) @updatedAt
  userId              String       @db.Uuid
  user                User         @relation(fields: [userId], references: [id])
  firstName           String?
}

Environment (please complete the following information):

  • Database kind: PostgreSQL
  • Node.js version: 18.14.6
  • @prisma/client version: 4.13.0
  • TypeScript version: 5.0.4

Thanks again for your hard work!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions