Home > database >  Missing field in available args
Missing field in available args

Time:04-07

I encountered a problem while trying to seed the user table.
I have a one-to-one relationship between the User and UserSettings model in it.

async function generateUsers() {
    const users = [];

    for (let i = 0; i < randomUsersCount; i  ) {
        users[i] = {
            first_name: faker.name.firstName(),
            last_name: faker.name.lastName(),
            email: faker.internet.email(),
            password: await hashPassword('testtest'),
            phone: faker.phone.phoneNumber('###-###-###'),
            role: 'USER',
            is_blocked: false,
            user_settings : {
                create: {
                    language: 'PL',
                    color: faker.internet.color(),
                }
            }
        };
    }

    await prisma.user.createMany({
        data: users,
    });
}

Error message:

Unknown arg `user_settings` in data.0.user_settings for type UserCreateManyInput. Available args:

type UserCreateManyInput {
  id?: Int
  first_name: String
  last_name: String
  email: String
  phone: String
  password: String
  role: UserRoles
  is_blocked?: Boolean
  created_at?: DateTime
  updated_at?: DateTime
}

but in my model the field exists:

model User {
  id            Int            @id @default(autoincrement())
  first_name    String
  last_name     String
  email         String         @unique
  phone         String
  password      String
  role          UserRoles
  is_blocked    Boolean        @default(false)
  created_at    DateTime       @default(now())
  updated_at    DateTime       @updatedAt
  user_settings UserSettings?
}

model UserSettings {
  id       Int       @id @default(autoincrement())
  language Languages
  color    String
  user_id  Int
  user     User      @relation(fields: [user_id], references: [id])
}

When I turn on Prisma Studio, I can see the column present in the table.
Where did I go wrong?

I have run npx prisma generate and npx prisma migrate and even npx prisma migrate reset many times, without success.

CodePudding user response:

You are trying to use nest createMany which isn't supported. This is from the documentation:

You cannot create or connect relations - you cannot nest create, createMany, connect, connectOrCreate inside a top-level createMany

As an alternative I would suggest to create users inside the loop while you are creating data.

async function generateUsers() {
  const users = [];

  for (let i = 0; i < randomUsersCount; i  ) {
      users[i] = {
          first_name: faker.name.firstName(),
          last_name: faker.name.lastName(),
          email: faker.internet.email(),
          password: await hashPassword('testtest'),
          phone: faker.phone.phoneNumber('###-###-###'),
          role: 'USER',
          is_blocked: false,
          user_settings : {
              create: {
                  language: 'PL',
                  color: faker.internet.color(),
              }
          }
      };

      await prisma.user.create({
        data: users[i],
    });
  }

}
  • Related