Optional Relation fields messing up Prisma Studio - prisma

I'm creating a notification system in my app, and I created a Notification model in Prisma to help handle this. It has two optional relation fields, optional because it can point to either a question or an answer, but not both, and not neither.
model Notification {
id Int #id #default(autoincrement())
for User #relation(fields: [userId], references: [id])
read Boolean #default(false)
question_reference Question? #relation(fields: [questionId], references: [id])
answer_reference Answer? #relation(fields: [answerId], references: [id])
userId Int
questionId Int?
answerId Int?
}
I'm trying to create a notification in Prisma Studio, but whenever I open it, I get an error. Looking at the logs, it is clear why this error is happening. Prisma Studio is making an invalid query.
{
"modelName": "Notification",
"operation": "findMany",
"args": {
"take": 100,
"skip": 0,
"select": {
"id": true,
"for": true,
"read": true,
"question_reference": true,
"answer_reference": true,
"userId": true,
"questionId": true,
"answerId": true
}
}
}
You can see the problem in the select statement. The two optional relation fields, which should have been in include are present here.
I didn't expect this new error, because I have used Prisma Studio in the past and never received this sort of error with any relation field. I tried upgrading to v4.10 but nothing has changed. I also tried running prisma migrate dev, but it returns "no schema change or pending migration was found".
Reproduction
On prisma studio, create a Notification model. This requires a previously created question and user model.
Select "Add Record", and fill in the parameters. You should use the already created User and Question records for the foreign key values.
You find the error detailed above

Related

Why can't I query certain fields in Prisma?

I need to be able to access certain fields of a Prisma model, but they always come back undefined even though I can see that the data exists in Prisma Studio.
Here is my model for Goods in schema.prisma:
model Good {
id String #id #default(cuid())
title String
completed Boolean #default(false)
createdAt DateTime #default(now())
updatedAt DateTime #updatedAt
user User #relation(fields: [userId], references: [id], onDelete: Cascade, onUpdate: Cascade)
userId String
}
Each Good has an id, title, etc. in addition to a user. However, when I log a Good to the console, it seems I can only access the title and id of the item. All the other expected fields are not present, and come back as undefined when I try to access them (i.e. console.log(good.user). Here's the output of console.log({good}):
{id: 'clcxxla0g0008ts5qjk248fzk', title: 'Title of good'}
What I need is to be able to query the user or userId for each Good, but I can't find a way to do this.
For more info, here is what what Prisma Studio data looks like:

explicit many to many self-relations produces weird results in the generated client, pairing up wrong values

I've got users and contact requests with a table in between to explicitly define a self relation for cascade purposes.
user schema:
contactRequestsReceivedFrom ContactRequestRelations[] #relation("contactRequestReceivedFrom")
contactRequestsMadeTo ContactRequestRelations[] #relation("contactRequestMadeTo")
relations schema:
model ContactRequestRelations {
contactRequestReceivedFrom User #relation("contactRequestReceivedFrom", fields: [requestReceivedFromId], references: [id], onDelete: Cascade)
contactRequestMadeTo User #relation("contactRequestMadeTo", fields: [requestMadeToId], references: [id], onDelete: Cascade)
requestReceivedFromId String
requestMadeToId String
##id([requestReceivedFromId, requestMadeToId])
}
• I have to give the relations a name, prisma will complain it's too ambiguous otherwise
• if I give the same relation reference on all 4 of them, prisma will complain of "wrongly named relation detected"
I wanted to make a 'send contact request' operation.
Where I find either the user that the request is made for and then add the relation to the 'contactRequestsReceivedFromarray ---- or ---- find the user that is sending the request and create a relation on thecontactRequestsMadeTo`.
The issue is, even though the relation contactRequestsReceivedFrom ContactRequestRelations[] #relation("contactRequestReceivedFrom") on the user is clearly paired with the contactRequestReceivedFrom User #relation("contactRequestReceivedFrom", fields: [requestReceivedFromId], references: [id], onDelete: Cascade) relation on the relation table, when I attempt to create this with the client, prisma only lets me define the opposite of that relation which is rather redundant and confusing:
as it would serve no purpose to define in that context, as I would be defining who made the request.
Is this the correct pattern/syntax, or should I be looking at a more complex model with a couple more tables in between? Not a big fan of that option.

After I run "npm run migrate", model is go away

I created model like this
model Comment {
id Int #id #default(autoincrement())
createdAt DateTime #default(now())
updatedAt DateTime #updatedAt
user User #relation(fields: [userId],references: [id])
photo Photo #relation(fields: [photoId],references: [id])
comment String
userId Int
photoId Int
}
After I run npm run migrate, the Comment Model is gone away... what should I do..?
Now, I see an error.
Error validating field photo in model Comment: The relation field photo on Model Comment is missing an opposite relation field on the model Photo. Either run prisma format or add it manually.

Is it possible to access metadata about the prisma model?

Let's say I have a model in my schema.prisma file:
model Post {
id Int #id #default(autoincrement())
author User #relation(fields: [authorId], references: [id])
authorId Int
}
Having a variable called model in my server containing a model name
const model: string = [model name generated dynamically]
Using this string I want to know every information about this model. For example if this variable model happens to be Post, I want to know that it has fields id, author, authorId and also information about each field separately, like in the case of author which field in which model does it reference, in this example the model User the field id.
I'm aware that prisma generates a type for each model and that way maybe I can access the fields that way but that't not enough for me, I want information about each fields as well.
I search the prisma docs, also googled something like 'get meta information about model in prisma2' but I didn't find any solution. Is there a way to achieve this?
Yes, you can get all the metadata for your entire schema in the following manner:
const prisma = new PrismaClient()
// #ts-ignore
console.log(prisma._dmmf)
This will give you all the details for the models and relations. The reason this is not documented is that this is highly experimental and will change with releases, which is why it's used for internal purposes only.

The Mongo connector currently does not support Cascading Deletes

I'm working with Prisma and I want to support CASCADE delete but although I have done everything mentioned in the docs I'm still not getting it to work
this is the error I'm getting when I try to Prisma deploy
Errors:
Restaurant
✖ The Mongo connector currently does not support Cascading Deletes, but the field `foods` defines cascade behavior. Please remove the onDelete argument.}
Here is the code
type Restaurant {
id: ID! #id
name: String!
foods: [Food!]! #relation(onDelete: CASCADE, name: "FoodToRestaurant", link: INLINE)
}
type Food {
id: ID! #id
name: String!
desc: String
price: Float!
category: Category!
restaurant: Restaurant! #relation(name: "FoodToRestaurant", onDelete: SET_NULL)
}
I expect when the restaurant gets deleted all its foods should also be deleted
I have CASCADE deleted with Prisma PostgreSQL but now I want to use MongoDB for this app
As it's not possible, you should manage it manually.
Means you should delete all of related entities recursively.
For example if it's your db schema:
Question -> Comment -> Rate
If you want to delete a question, you should delete all of comments related to that question, and if you want to delete a comment, you should delete all of rates assigned to that comment. So you need some recursive functions for deleting these entities.
function deleteQuestion(questionId) {
for each comment related to questionID
deleteComment(commentId)
delete(questionId)
}
function deleteComment(commentId) {
for each rate related to commentId
deleteRate(rateId)
delete(commentId)
}
function deleteRate(rateId) {
delete(rateId)
}
currently mongodb prisma not support cascading delete. if you have it in your schema please remove it.