How to do relationship updates? Prisma fails when doing connection to relationship.
One entity has relationship to another entity (table relationship). Per prisma docs I should be able to update it like this from .update method:
data: {
status: status,
city: {
connect: {
id: cityId
}
}
So city is the relationship, cityId is a column in user as a FK. I tried other variations such as setting cityId: cityId directly. It won't work. Auto generated and compiled types are a mess and I think that is the reason.
13 Replies
Hi @neogeofun. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Based on the knowledge sources provided, there isn't specific information about how to update relationships in Prisma when using Wasp. However, there are mentions of issues related to updating Prisma to newer versions, which might solve some problems with type safety and other features.
For example, in issue #1570 (https://github.com/wasp-lang/wasp/issues/1570), it's mentioned that upgrading to Prisma 5.X could potentially solve some type safety issues. In issue #359 (https://github.com/wasp-lang/wasp/issues/359) and issue #194 (https://github.com/wasp-lang/wasp/issues/194), there are discussions about updating Prisma from v2 to v3 and to the latest version (2.17.0) respectively.
In issue #1312 (https://github.com/wasp-lang/wasp/issues/1312), there's a suggestion to set
previewFeatures = ["extendedWhereUnique"]
by default for Prisma, which enables update()
and similar functions in Prisma to take more than just unique fields.
However, without more specific information or code examples related to your issue, it's hard to provide a more detailed solution. I would recommend checking the Prisma documentation or seeking further assistance in the Wasp community.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @neogeofun, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?Error is like this if I try to directly update the cityId,
But if I try to connect city with:
Wohooo @neogeofun, you just became a Waspeteer level 1!
I still get the same error and then it will say unknown arg city did you mean cityId...
@neogeofun can you please share the entities in question from
main.wasp
? So we can see what kind of Prisma Schema we are dealing with.
Also, what did you mean with autogenerated and compiled types being a mess?I solved it by converting the type expectation to any in the action file. There is an issue related to types on the issue tracker, I think it is sort of a similar problem. I have seen Prisma generated types cause issues with operations in other projects as well, and in this case, types fields have non-scalar types as well. So, I think it is likely a Prisma issue if it is all handled under the hood by Prisma. It is also possible I don't remember some things Prisma related. But in this instance, I tried everything. It is something like this:
All I was doing was trying to update a Store's city relation as I do other updates. So tried to directly update cityId and it would complain about type not fitting and ask me if I meant "city" when I tried to update by using
connection
the same message returns except it asks if I meant to update cityId field. Now I removed the type from parameters (yeah not the best thing to do but I am just building an MVP and trying this). Making it any fixed it, by just updating the cityId Prisma automatically updates the relation as well, as it should. But if I used types from "wasp/entities" it just wouldn't work.When you updated the
Store
or City
entities did you run the wasp db migrate-dev
command afterwards?
First let's get the small issues out of the way 😄
Trying out your models gives me this Prisma error:
Did you also get it maybe? You can fix it by adding a stores Store[]
to the City
entity.
After we fix that ... let's see if I can use the Prisma Client with the city
relation.I've tried doing this and it seems to work 🎊 :
There can be some transient errors with the Typescript intellisense which make it seem like there is a type error but there isn't.
If you suspect something is off with the types, you can simply run Restart TypeScript server in VS Code with a command to see if it helps (it usually does).
Also, you need to make sure to run
wasp db migrate-dev
after you change your DB schema to make sure the Prisma Client gets updated.Here's my Wasp file for reference:
Let me know if this works for you in the end, if not, let's figure out what is wrong 🙂
Thank you for the detailed response. Yes, I did the migration I am pretty sure.
Wohooo @neogeofun, you just became a Waspeteer level 2!
Did things start working for you in the end?
Yes, the issue was caused by an input type mismatch. Unfortunately, I resorted to using any in this instance. My goal is to just finish an MVP and see if it goes well, can fix type issues at another time.