Title
k

Kha

03/18/2021, 7:00 AM
Having some trouble with updating data with some nested creates and connects. Can someone help?
My Models
My models:

model User {
  id                              String                            @id @map("user_id")
  email                           String                            @unique
  location       Location? @relation(fields: [locationID], references: [id])
  locationID     Int?
}

model Location {
  id        Int     @id @default(autoincrement())
  label     String  @unique
  placeID   String  @unique
  city      City    @relation(fields: [cityID], references: [id])
  state     State?  @relation(fields: [stateID], references: [id])
  country   Country @relation(fields: [countryID], references: [id])
  cityID    Int
  stateID   Int?
  countryID Int
  User      User[]
}
My function call that is giving me an error:
await this.prisma.user.update({
      where: {
        id: userID,
      },
      data: {
        location: {
          connectOrCreate: {
            where: {
              label,
            },
            create: {
              label,
              placeID,
              city: {
                connectOrCreate: {
                  where: {
                    name: city,
                  },
                  create: {
                    name: city,
                  },
                },
              },
              state: {
                connectOrCreate: {
                  where: {
                    name: state,
                  },
                  create: {
                    name: state,
                  },
                },
              },
              country: {
                connectOrCreate: {
                  where: {
                    name: country,
                  },
                  create: {
                    name: country,
                  },
                },
              },
            },
          },
        },
      },
      include: {
        location: true,
      },
    });
  }
The error says that the
location
field doesn’t exist and suggests that maybe I meant locationID instead
Unknown arg `location` in data.location for type UserUncheckedUpdateInput. Did you mean `locationID`
m

Manish

03/18/2021, 7:12 AM
Did you rebuild your prisma client?
k

Kha

03/18/2021, 7:12 AM
I have been running
npx prisma generate
So it should be updated
m

Manish

03/18/2021, 7:14 AM
In a update query, would you need include?
k

Kha

03/18/2021, 7:14 AM
I don’t. I get the same error regardless of the include being there
My main issue right now is that it for some reason doesn’t let me target the attribute
location
, only
locationID
But I should be able to have
location
there right?
m

Manish

03/18/2021, 7:16 AM
Yes, your code looks fine actually.
Just wondering if you faced the same problem in create also?
k

Kha

03/18/2021, 7:17 AM
Let me see
m

Manish

03/18/2021, 7:17 AM
That is create a user using prisma.user.create and add location also along with the user.
k

Kha

03/18/2021, 7:21 AM
Okay I get the same error when I try to create a new user as well (instead of update)
This time it’s saying
UserUncheckedCreateInput
I wonder if that is the correct type that should be applied when creating a new user
m

Manish

03/18/2021, 7:23 AM
@Ryan can you help?
k

Kha

03/18/2021, 7:24 AM
Because it only gives me access to
locationID
instead of the
location
object
m

Manish

03/18/2021, 7:24 AM
That should not happen. We’re missing something in the model. Let’s wait for Ryan to respond.
r

Ryan

03/18/2021, 7:31 AM
@Kha I tested your query passing static values and it works fine. Make sure you’re not passing anything
undefined
or
null
and check.
k

Kha

03/18/2021, 7:39 AM
Hey @Ryan I checked everything I’m passing in isn’t null
Let me send a bigger log of the error. Note this will contain more attributes than I originally mentioned since I was providing a minimum representation
Error: 
Invalid `prisma.user.update()` invocation:

{
  where: {
    id: '3fa015c4-d06b-4413-adb4-5ffb2bc31722'
  },
  data: {
    location: {
    ~~~~~~~~
      connectOrCreate: {
        where: {
          label: 'Tokyo, Japan'
        },
        create: {
          label: 'Tokyo, Japan',
          placeID: '1234',
          city: {
            connectOrCreate: {
              where: {
                name: 'Tokyo'
              },
              create: {
                name: 'Tokyo'
              }
            }
          },
          state: {
            connectOrCreate: {
              where: {
                name: undefined
              },
              create: {
                name: undefined
              }
            }
          },
          country: {
            connectOrCreate: {
              where: {
                name: 'Japan'
              },
              create: {
                name: 'Japan'
              }
            }
          }
        }
      }
    }
  }
}

Unknown arg `location` in data.location for type UserUncheckedUpdateInput. Did you mean `locationID`? Available args:
type UserUncheckedUpdateInput {
  id?: String | StringFieldUpdateOperationsInput
  email?: String | StringFieldUpdateOperationsInput
  password_hash?: String | StringFieldUpdateOperationsInput
  time_joined?: BigInt | BigIntFieldUpdateOperationsInput
  username?: String | NullableStringFieldUpdateOperationsInput | Null
  name?: String | NullableStringFieldUpdateOperationsInput | Null
  profilePicture?: String | NullableStringFieldUpdateOperationsInput | Null
  bio?: String | NullableStringFieldUpdateOperationsInput | Null
  dateOfBirth?: DateTime | NullableDateTimeFieldUpdateOperationsInput | Null
  locationID?: Int | NullableIntFieldUpdateOperationsInput | Null
  emailpassword_pswd_reset_tokens?: emailpassword_pswd_reset_tokensUncheckedUpdateManyWithoutUserInput
}


    at Document.validate (/Users/khanguyen/Desktop/Projects/codusk-api/node_modules/@prisma/client/runtime/index.js:32448:19)
    at PrismaService._executeRequest (/Users/khanguyen/Desktop/Projects/codusk-api/node_modules/@prisma/client/runtime/index.js:34385:17)
    at /Users/khanguyen/Desktop/Projects/codusk-api/node_modules/@prisma/client/runtime/index.js:34320:52
    at AsyncResource.runInAsyncScope (async_hooks.js:197:9)
    at PrismaService._request (/Users/khanguyen/Desktop/Projects/codusk-api/node_modules/@prisma/client/runtime/index.js:34320:25)
    at Object.then (/Users/khanguyen/Desktop/Projects/codusk-api/node_modules/@prisma/client/runtime/index.js:34440:39)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
Oh wait.
state
is undefined
silly me
Anyone know of an easy way to omit the part of assigning the
state
if a state is not provided?
Wondering if there is some easy shorthand way in JS/TS rather than having to do an if/else statement and then have duplicate code
I got it 🙂 I did
state: state
                ? {
                    connectOrCreate: {
                      where: {
                        name: state,
                      },
                      create: {
                        name: state,
                      },
                    },
                  }
                : undefined,
👏 1
💯 1