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

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.

Related

In Prisma, how to specify 1-to-n relation where always n>0?

The Prisma data model of my app has Posts, and each Post has a number of Authors. They can't have zero authors. As I understand it, the following model:
model Post {
id Int #id #default(autoincrement())
title String
authors Author[] #relation(references: [id])
}
allows a Post to have zero Authors. I'm new to Prisma. Can my data model be changed to reflect that a Post needs at least one Author?

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:

Ambiguous self relation detected [duplicate]

I am trying to create a prisma schema for a self relation. I would like to be able to show related posts for each post. Here is my post model:
model Post {
id Int #id #default(autoincrement())
title String
content String
relatedPosts Post[]
}
I am unsure on how to get this to work, the error message I am getting says that I need to define the other side of the relationship.
You are trying to create a many-to-many self relation. In such cases, you need to create two fields:
One field to represent the suggested posts for a given parent post (named relatedPosts in the example)
One field to represent the parent for a given suggested post (named relatedPostParent in the example)
This is what the syntax will look like
model Post {
id Int #id #default(autoincrement())
title String
content String
relatedPosts Post[] #relation("RelatedPosts", references: [id]) // child post (post that are suggested)
relatedPostParent Post[] #relation("RelatedPosts", references: [id]) // parent post (parent post of a suggested post)
}
Note that this is an implicit many-to-many relation. The article linked at the top also shows how to create an explicit many-to-many relation, if that is what you'd prefer.

Conditional unique contstraint in prisma/postgres schema? Is it possible?

I am trying to implement an AuthProvider model for our prisma schema, using postgres as the provider. You can see the start of my work here:
model AuthProvider {
id String #id #default(cuid())
createdAt DateTime #default(now())
updatedAt DateTime #updatedAt
type AuthProviderType
externalUserId String? #unique
email String #unique // this should not be unique if the userId is the same among two AuthProviders
emailVerified Boolean? #default(false)
password String? // Hashed password
user User #relation(fields: [userId], references: [id])
userId String
##map("auth_providers")
}
A user can have many authentication providers, such as email + password, google, twitter. They can sign in with whatever they choose once they have connected it
Now here comes the tricky part:
what I want to accomplish is to make it so an AuthProviders email is unique, but ONLY if the userId of that AuthProvider is different.
So a user should be able to have several AuthProviders with the same email, but if one user tries to connect an authprovider with an email that another user already has connected (on any of their authproviders), then it should fail via unique constraint.
would this be achieveable just using unique constraints/indexes in the schema?

Prisma self relation to display related posts

I am trying to create a prisma schema for a self relation. I would like to be able to show related posts for each post. Here is my post model:
model Post {
id Int #id #default(autoincrement())
title String
content String
relatedPosts Post[]
}
I am unsure on how to get this to work, the error message I am getting says that I need to define the other side of the relationship.
You are trying to create a many-to-many self relation. In such cases, you need to create two fields:
One field to represent the suggested posts for a given parent post (named relatedPosts in the example)
One field to represent the parent for a given suggested post (named relatedPostParent in the example)
This is what the syntax will look like
model Post {
id Int #id #default(autoincrement())
title String
content String
relatedPosts Post[] #relation("RelatedPosts", references: [id]) // child post (post that are suggested)
relatedPostParent Post[] #relation("RelatedPosts", references: [id]) // parent post (parent post of a suggested post)
}
Note that this is an implicit many-to-many relation. The article linked at the top also shows how to create an explicit many-to-many relation, if that is what you'd prefer.