Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support natural ordering for many to many relationship #23898

Closed
conduongtong opened this issue Apr 19, 2024 · 2 comments
Closed

Support natural ordering for many to many relationship #23898

conduongtong opened this issue Apr 19, 2024 · 2 comments
Labels
kind/feature A request for a new feature. team/client Issue for team Client. tech/engines/query engine Issue in the Query Engine tech/engines Issue for tech Engines. topic: implicit m:n implicit many-to-many relations topic: m:n many-to-many relations

Comments

@conduongtong
Copy link

conduongtong commented Apr 19, 2024

Problem

You can read the problem in here: #22561
This problem causes incorrect ordering in one-to-many and many-to-many relationships.

Suggested solution

Maybe when using:

await prisma.post.findFirst({ include: { tags: true } });

It will return id of many to many relation table instead of sorting id of A or B
image

@Weakky Weakky changed the title Support Order Many to Many Relationship Support natural ordering for many to many relationship Apr 24, 2024
@Weakky Weakky added kind/feature A request for a new feature. team/client Issue for team Client. tech/engines Issue for tech Engines. tech/engines/query engine Issue in the Query Engine topic: m:n many-to-many relations labels Apr 24, 2024
@janpio
Copy link
Member

janpio commented Apr 29, 2024

Without an explicit orderBy you will not get any guarantees by Prisma in which order the data is returned.

The primary key here also has no explicit meaning, as when you create the data as shown in the discussion with await prisma.post.update({ where: { id: 1 }, data: { tags: { connect: [{ id: 2 }, { id: 1 }, { id: 3 }] } } }); there is no guarantee in which the data will be created, and hence the primary keys assigned.

Do you have example of other tools that show how they achieve what you suggest?

@janpio janpio added the topic: implicit m:n implicit many-to-many relations label Apr 30, 2024
@janpio
Copy link
Member

janpio commented Apr 30, 2024

After asking my colleagues internally, I am closing this issue now as we all agree that Prisma can not guarantee any order unless you ask it to order the data a certain way. Additionally, for implicit m:n relations there is no way to add such an order field - so you would need to fall back to using en explicit m:n relation where you have that control and then can also use orderBy.

(Happy to continue the discussion in the closed issue of course, I don't want to silence your need - just want to make it clear how we currently think about this. Thanks.)

@janpio janpio closed this as not planned Won't fix, can't repro, duplicate, stale Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature A request for a new feature. team/client Issue for team Client. tech/engines/query engine Issue in the Query Engine tech/engines Issue for tech Engines. topic: implicit m:n implicit many-to-many relations topic: m:n many-to-many relations
Projects
None yet
Development

No branches or pull requests

3 participants