W
Wasp-lang•6mo ago
neogeofun

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
kapa.ai
kapa.ai•6mo ago
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?
neogeofun
neogeofunOP•6mo ago
Error is like this if I try to directly update the cityId,
Unknown arg `cityId` in data.cityId for type ApplicationUpdateInput. Did you mean `city`? Available args:...
Unknown arg `cityId` in data.cityId for type ApplicationUpdateInput. Did you mean `city`? Available args:...
But if I try to connect city with:
city: {
connect: {
id: cityId,
}
}
city: {
connect: {
id: cityId,
}
}
MEE6
MEE6•6mo ago
Wohooo @neogeofun, you just became a Waspeteer level 1!
neogeofun
neogeofunOP•6mo ago
I still get the same error and then it will say unknown arg city did you mean cityId...
martinsos
martinsos•6mo ago
@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?
neogeofun
neogeofunOP•6mo ago
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:
entity Store {=psl
id String @id @default(uuid())
status String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id])
userId String
city City? @relation(fields: [cityId], references: [id])
cityId Int?
}

entity City {=psl
id Int @id @default(autoincrement())
name String @unique
location String
description String
website String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
logoLink String
psl=}
entity Store {=psl
id String @id @default(uuid())
status String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id])
userId String
city City? @relation(fields: [cityId], references: [id])
cityId Int?
}

entity City {=psl
id Int @id @default(autoincrement())
name String @unique
location String
description String
website String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
logoLink String
psl=}
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.
miho
miho•6mo ago
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:
[ Db !] error: Error validating field `city` in model `Store`: The relation field `city` on model `Store` is missing an opposite relation field on the model `City`. Either run `prisma format` or add it manually.
[ Db !] --> schema.prisma:23
[ Db !] |
[ Db !] 22 | userId String
[ Db !] 23 | city City? @relation(fields: [cityId], references: [id])
[ Db !] 24 | cityId Int?
[ Db !] |
[ Db !]
[ Db !] Validation Error Count: 1
[ Db !] [Context: getDmmf]
[ Db !]
[ Db !] Prisma CLI Version : 4.16.2
[ Db !] error: Error validating field `city` in model `Store`: The relation field `city` on model `Store` is missing an opposite relation field on the model `City`. Either run `prisma format` or add it manually.
[ Db !] --> schema.prisma:23
[ Db !] |
[ Db !] 22 | userId String
[ Db !] 23 | city City? @relation(fields: [cityId], references: [id])
[ Db !] 24 | cityId Int?
[ Db !] |
[ Db !]
[ Db !] Validation Error Count: 1
[ Db !] [Context: getDmmf]
[ Db !]
[ Db !] Prisma CLI Version : 4.16.2
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.
miho
miho•6mo ago
I've tried doing this and it seems to work 🎊 :
import { type SomeAction } from "wasp/server/operations";

type SomeActionInput = void;
type SomeActionOutput = void;

export const createSomeStore: SomeAction<
SomeActionInput,
SomeActionOutput
> = async (args, context) => {
await context.entities.Store.create({
data: {
status: "OPEN",
user: {
connect: {
id: "someid",
},
},
city: {
connect: {
id: 123,
},
},
},
});
};
import { type SomeAction } from "wasp/server/operations";

type SomeActionInput = void;
type SomeActionOutput = void;

export const createSomeStore: SomeAction<
SomeActionInput,
SomeActionOutput
> = async (args, context) => {
await context.entities.Store.create({
data: {
status: "OPEN",
user: {
connect: {
id: "someid",
},
},
city: {
connect: {
id: 123,
},
},
},
});
};
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.
No description
miho
miho•6mo ago
Here's my Wasp file for reference:
app prismaStuff {
wasp: {
version: "^0.13.2"
},
title: "prisma-stuff"
}

route RootRoute { path: "/", to: MainPage }
page MainPage {
component: import { MainPage } from "@src/MainPage"
}

entity User {=psl
id String @id @default(uuid())
stores Store[]

psl=}

entity Store {=psl
id String @id @default(uuid())
status String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id])
userId String
city City? @relation(fields: [cityId], references: [id])
cityId Int?
psl=}

entity City {=psl
id Int @id @default(autoincrement())
name String @unique
location String
description String
website String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
logoLink String
stores Store[]
psl=}

action someAction {
fn: import { createSomeStore } from "@src/actions",
entities: [Store]
}
app prismaStuff {
wasp: {
version: "^0.13.2"
},
title: "prisma-stuff"
}

route RootRoute { path: "/", to: MainPage }
page MainPage {
component: import { MainPage } from "@src/MainPage"
}

entity User {=psl
id String @id @default(uuid())
stores Store[]

psl=}

entity Store {=psl
id String @id @default(uuid())
status String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id])
userId String
city City? @relation(fields: [cityId], references: [id])
cityId Int?
psl=}

entity City {=psl
id Int @id @default(autoincrement())
name String @unique
location String
description String
website String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
logoLink String
stores Store[]
psl=}

action someAction {
fn: import { createSomeStore } from "@src/actions",
entities: [Store]
}
Let me know if this works for you in the end, if not, let's figure out what is wrong 🙂
neogeofun
neogeofunOP•6mo ago
Thank you for the detailed response. Yes, I did the migration I am pretty sure.
MEE6
MEE6•6mo ago
Wohooo @neogeofun, you just became a Waspeteer level 2!
miho
miho•6mo ago
Did things start working for you in the end?
neogeofun
neogeofunOP•5mo ago
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.
Want results from more Discord servers?
Add your server