© 2026 Hedgehog Software, LLC

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

Using Query Syntax to filter by FK's field

hey there, I have a schema of users and their pets name and type. a user can only own one pet, and a pet could have several owners. how would I use query syntax to get all the users that own a dog (type === 'dog')?
export const users = pgTable('users', {
    id: serial('id').primaryKey(),
    petId: text('petId'),
        name: text('name'),
});
export const pets = pgTable('pets', {
    id: serial('id').primaryKey(),
    name: text('name'),
        type: text('type'),
});

export const usersRelations = relations(users, ({ one }) => ({
    pet: one(pets, { fields: [user.petId], references: [pet.id] }),
        
}));
export const users = pgTable('users', {
    id: serial('id').primaryKey(),
    petId: text('petId'),
        name: text('name'),
});
export const pets = pgTable('pets', {
    id: serial('id').primaryKey(),
    name: text('name'),
        type: text('type'),
});

export const usersRelations = relations(users, ({ one }) => ({
    pet: one(pets, { fields: [user.petId], references: [pet.id] }),
        
}));
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

Filter items by field on relation using query format
Drizzle TeamDTDrizzle Team / help
6mo ago
How to filter by joined tables with Drizzle query syntax?
Drizzle TeamDTDrizzle Team / help
14mo ago
How can I filter on a joined table field (nested value) with the relational query syntax?
Drizzle TeamDTDrizzle Team / help
3y ago
How to query join table with db.query syntax?
Drizzle TeamDTDrizzle Team / help
15mo ago