Ambiguous self relation detected [duplicate] - prisma

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.

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?

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.

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.

Database first - EF created objects ignoring the name of foreign keys

I have created a mssql database and then used these instructions to create the datamodel. I have noticed something weird and it is also something which doesn't allow me to work with the model without confusion.
One of my objects is Book. Book contains an id, name, isbn, .. and also references to a Person table, like the author, the editor and the publisher.
the created model contains
string name;
int id;
string isbn;
int authorid;
int editorid;
int publisherid;
person person1;
person person2;
person person3;
Is there a way to change the names of the persons at creation? Because otherwise it is hard to figure out which person is who and I will have to change a lot manually.

Grails one to many relationship

In Grails if there is a many to many relation ships for example book has many authors
Lets says book1 has author1,autho2,author3,author4 values .Now a PUT request is sent saying that book1 has only author1,author3 then the other two values are deleted from the table.
Now for the same scenario with one to many relation ship lets say book1 has author1,autho2,author3,author4 values now if a PUT request is done with only author1,author3
Is it suppose to delete the other two i.e, author2 and author4 values?? i expect the behavior to be so..
Below is my model for book and author
class Author {
String name;
static hasMany = [books: Book]
static mapping = {
books cascade: 'all-delete-orphan'
}
}
class Book{
String name
static belongsTo = [author: Author]
}
EDIT:
When i emplement the all-delete-orphan i get the followinh error
A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance
It depends on the cascade options set. If you are using belongsTo on the Author table grails uses cascade all by default. This means that the Author objects are removed if they are not used by any other Book. You can customize this behaviour using the cascade option (all-delete-orphan should be the value you are interested in).