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

[FEATURE]: "One of many" relationship #839

Closed
MatanYadaev opened this issue Jul 1, 2023 · 4 comments
Closed

[FEATURE]: "One of many" relationship #839

MatanYadaev opened this issue Jul 1, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@MatanYadaev
Copy link

Describe what you want

A model can have a one-to-many relation, but sometimes you might want just the "latest" or the "oldest" record of this relation. "Has one of many" relationship helps to accomplish that.

const authorsRelations = relations(authors, ({ many }) => ({
  books: many(books),

  // Feature request:
  latestBook: latestOneOfMany(books),
  oldsetBook: oldestOneOfMany(books),
  cheapestBook: oneOfMany(books, [
    { column: 'price', function: 'max' },
    { column: 'id', function: 'max' },
  ]),
});

Prior Art

@MatanYadaev MatanYadaev added the enhancement New feature or request label Jul 1, 2023
@erfanium
Copy link

@MatanYadaev
You can create a custom sql view for that. like author_latest_books.
then your author table has a one-to-one relationship with author_latest_books.

@MatanYadaev
Copy link
Author

@erfanium Of course I can do stuff manually, but I prefer the ORM to do that for me. Thanks :)

@erfanium
Copy link

erfanium commented Aug 9, 2023

@MatanYadaev It's a bit off topic: ORMs can be incredible unmaintainable, if you say yes to every feature request. that's why most ORMs suck and they will be abandoned after a while.

you start with latestOneOfMany and oldestOneOfMany. then someone else will come and say i need maxOneOfMany and minOneOfMany. then another guy says i need countOfMany and averageOfMany. this simply never ends.

I'm not a drizzle-orm contributor currently, but personally i don't want it to go that way, like other similar projects.

@L-Mario564
Copy link
Collaborator

Closing this in favor of #821. The feature request in the mentioned issue would lead to a more dynamic implementation of what's described here.

@L-Mario564 L-Mario564 closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants