© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•2y ago•
3 replies
Jeno

In a relation subquery, how to reference parent column?

I have this query:

    const result = await db.query.author.findFirst({
      where: (author) => eq(author.slug, params.slug),
      with: {
        collections: {
          with: {
            collection: {
              with: {
                books: {
                  with: {
                    book: {
                      with: {
                        authors: {
                          where: (authorBook, { ne }) => ne(authorBook.authorId, author.id),
                          with: {
                            author: true
                          }
                        }
                      }
                    }
                  }
                },
                authors: {
                  with: {
                    author: true
                  }
                }
              }
            }
          }
        }
      }
    });
    const result = await db.query.author.findFirst({
      where: (author) => eq(author.slug, params.slug),
      with: {
        collections: {
          with: {
            collection: {
              with: {
                books: {
                  with: {
                    book: {
                      with: {
                        authors: {
                          where: (authorBook, { ne }) => ne(authorBook.authorId, author.id),
                          with: {
                            author: true
                          }
                        }
                      }
                    }
                  }
                },
                authors: {
                  with: {
                    author: true
                  }
                }
              }
            }
          }
        }
      }
    });


Basically I want to query an author, with collections, and collections have books, books can have multiple authors. I want to query only other book authors where they are not the main author. The query doesn't error out but it includes the main author on every book. What am I missing?
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

How to select specific fields in subquery and reference it in parent query?
Drizzle TeamDTDrizzle Team / help
2y ago
Circular reference in subquery
Drizzle TeamDTDrizzle Team / help
2y ago
How to Reference a Column Type in Drizzle
Drizzle TeamDTDrizzle Team / help
2y ago
how to express the "children" relation in parent/child?
Drizzle TeamDTDrizzle Team / help
3y ago