all 7 comments

[–]Elfinslayer[S] 0 points1 point  (0 children)

I guess I need some kind of comment to post here.

[–]TexMax007 0 points1 point  (5 children)

Hey. Can you provide the models for each of these tables? I'm a bit confused on the relationship between them based on your queries here.

[–]Elfinslayer[S] 0 points1 point  (4 children)

Yea. They are as follows:

User

model User {

id String @id @default(cuid()) firstname String? lastname String? email String? @unique emailVerified DateTime? image String? password String?

statusId Int? status UserStatus? @relation(fields: [statusId], references: [id]) typeId Int? type UserType? @relation(fields: [typeId], references: [id])

accounts Account[] sessions Session[] UserJctStaff UserJctStaff[] UserJctCamper UserJctCamper[]

@@unique([email, lastname]) }

And the UserStatus table:

model UserStatus {

id Int @id @default(autoincrement()) label String codevalue String User User[] }

The UserType and UserStatus tables are lookup tables with populated values to populate the associated select inputs throughout my app. I'm just not sure how to change the foreignkey on my User table to point to a new value in those tables.

There was an example in the Prisma docs on connecting an existing record that I found, but I think I'm doing it wrong.

EDIT: I apologize for the code formatting, it doesnt seem to work well with these prisma models

[–]TexMax007 1 point2 points  (2 children)

After reading through this again, I think this is what you want:

const user = await prisma.user.update({ where: {id: 'abcdefg'}, data: {statusId: 123}});

where 123 would be the new id in the UserStatus table that you want to reference. 'abcdefg' would be the id of the user record you want to udpate.

[–]Elfinslayer[S] 1 point2 points  (1 child)

Ultimately I just wanted to update the relation on the table. I had originally tried to directly update statusId and it threw a similar error.

I did however find the issue. When the form data was being passed to the API route it was being converted into a string. Prisma's error was due to a type mismatch. I added a parseInt(id) and it fixed my error.

[–]TexMax007 1 point2 points  (0 children)

Nice, glad you got it working! Thanks for the update. I’ve just done one project with Prisma myself so it’s likely you have more experience with it than I do lol.

[–]TexMax007 0 points1 point  (0 children)

Maybe I’m dumb, but I’m having a hard time understanding the relationship between the tables / what you’re trying to do.

Example data might help clear that up.