Prisma client: Can't provide Id for reference

Viewed 234

Having issue with inserting an employee and a user that have a relationship (only in the db ;))

The principle: A user can create multiple employees and then give them access by adding them as a user. An employee can have a user account. A user can be an employee.

Only one user account per employee. Also, by putting the password in a different table I hope to mitigate some accidental leaks where the password might be sent to the client, etc. [I still need to encrypt email and phone number in the db]

My Prisma model:

model User {
  id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid

  username      String @unique
  password_hash Bytes

  employee   Employee? @relation("EmployeeUserInfo")
  employeeId String    @unique @db.Uuid

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  employeesAdded Employee[] @relation("EmployeeAddedByUser")

}

model Employee {
  id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid

  first_name   String
  last_name    String
  phone_number String

  user   User?  @relation("EmployeeUserInfo", fields: [userId], references: [id])
  userId String @unique @db.Uuid

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  addedBy   User?  @relation("EmployeeAddedByUser", fields: [addedById], references: [id])
  addedById String @db.Uuid
}

My Prisma client code:

    // let user = await prisma.user.create({
    //     data: {
    //         username: 'system',
    //         password_hash: Buffer.from(hash),
    //         employee: {
    //             create: {
    //                 first_name: 'system',
    //                 last_name: 'system',
    //                 phone_number: '5555555555'
    //             }
    //         },
    //     },

    // });
    let user = await prisma.employee.create({
        data: {
            first_name: 'system',
            last_name: 'system',
            phone_number: '5555555555',
            user: {
                create: {
                    username: 'system',
                    password_hash: Buffer.from(hash),
                }
            },
        },
        select: {
            user: true
        }

    });

The error I receive is:

Invalid `prisma.employee.create()` invocation:

{
  data: {
    first_name: 'system',
    last_name: 'system',
    phone_number: '5555555555',
    user: {
      create: {
        username: 'system',
        password_hash: Buffer(3),
+       employeeId: String,
?       id?: String,
?       createdAt?: DateTime,
?       updatedAt?: DateTime,
?       employeesAdded?: {
?         create?: EmployeeCreateWithoutAddedByInput | EmployeeCreateWithoutAddedByInput | EmployeeUncheckedCreateWithoutAddedByInput | EmployeeUncheckedCreateWithoutAddedByInput,
?         connectOrCreate?: EmployeeCreateOrConnectWithoutAddedByInput | EmployeeCreateOrConnectWithoutAddedByInput,
?         createMany?: EmployeeCreateManyAddedByInputEnvelope,
?         connect?: EmployeeWhereUniqueInput | EmployeeWhereUniqueInput
?       }
      }
    }
  },
  select: {
    user: true
  }
}

Argument employeeId for data.user.create.employeeId is missing.

Note: Lines with + are required, lines with ? are optional.

I tried in both directions: User creates employee and Employee creates user.

With the commented code, I get a similar message Argument employeeId for data.employeeId is missing..

I guess I can't make this work because they both reference each other (even if they are optional)? If so, what would make the most sense from a security/db perspective? I saw a suggestion about using select to return the id but unless I'm not using it the right way, it doesn't work.

Using Nexus-Prisma in the background, in case that helps.

0 Answers
Related