I have a schema that looks like this: ```model Use...
# orm-help
h
I have a schema that looks like this:
Copy code
model User {
  id          Int       @id @default(autoincrement())
  createdAt   DateTime  @default(now())
  username    String    @unique
  password    String
  userUUID    UserUUID?
}

model UserUUID {
  id     String @id @default(uuid())
  user   User   @relation(fields: [userId], references: [id])
  userId Int    @unique
}
I want to add an entry to UserUUID if one is not present for the provided username, how can i achieve that? I tried with the following but it complains about not being able to uniquely identify a user.
Copy code
await prisma.userUUID.upsert({
        where: {
            user: {
                username: username.toLowerCase()
            }
        },
        update: {},
        create: {
            user: {
                connect: {
                    username: username.toLowerCase()
                }
            }
        }            
    });
Anyone knows how i can acheive this? Reason for having a UserUUID is that it should not always be present for every user, only users that have activated that feature.
r
@Halvor 👋 The code you specified above works for me. Could you check if the
username
passed is correct and present in the database?
h
@Ryan Mhm, it shows an error in VSCode with the Prisma plugin. I can double check if i am i am able to run it.
It is possible to use User to uniquely identify UserUUID, even though the 1-to-1 relation is only required in the UserUUID table, but optional in User table? I will test the code again when i am back from work.
r
In this case,
upsert
will only work for the field in the same model. It cannot reference the
@unique
fields in another model.
h
@Ryan I am confused, will it add a row to the UserUUID table? I get that it cannot change username or any other properties of User table.
r
It will add a new row to the UserUUID table as you’re using
create
.
h
Thx for help :-)
👍 1
@Ryan How were you able to get this code to run? I'm just getting the following error:
Copy code
Compilation error in /srv/rest/src/services/gator/gatorCommon.ts
rest_1      | [ERROR] 14:42:26 ⨯ Unable to compile TypeScript:
rest_1      | src/services/gator/gatorCommon.ts(25,13): error TS2322: Type '{ user: { username: string; }; }' is not assignable to type 'UserUUIDWhereUniqueInput'.
rest_1      |   Object literal may only specify known properties, and 'user' does not exist in type 'UserUUIDWhereUniqueInput'.
Were you able to select based on
Copy code
where: {
    user: {
        username: "someusername"
    }
}
How did you make this work, from my understanding from what you mentioned earlier it is not possible to do this? As you cannot reference another 1-to-1 model relationship as unique in UserUUID model?
@Ryan Maybe this conversation dissapeared among all the other stuff, but i am interested to know how you managed to get it working :)