-
Imagine that I have an Item and Collection: export const itemsTable = sqliteTable('items', {
id: integer('id', { mode: 'number' }).primaryKey({ autoIncrement: true }),
name: text('name').notNull(),
});
export const collectionsTable = sqliteTable('collections', {
id: integer('id', { mode: 'number' }).primaryKey({ autoIncrement: true }),
name: text('name').notNull(),
}); I want to put items in collections, so I created a table to store the relations between them: export const collectionsThingsTable = sqliteTable(
'collections_items',
{
id: integer('id', { mode: 'number' }).primaryKey({ autoIncrement: true }),
collectionId: integer('collection_id', { mode: 'number' })
.notNull()
.references(() => collectionsTable.id, { onDelete: 'cascade' }),
itemId: integer('item_id', { mode: 'number' })
.notNull()
.references(() => itemsTable.id, { onDelete: 'cascade' }),
}
);
export const itemRelations = relations(itemsTable, ({ many }) => ({
collections: many(collectionsThingsTable),
}));
export const collectionsRelations = relations(collectionsTable, ({ many }) => ({
items: many(collectionsThingsTable),
}));
export const collectionsThingsRelations = relations(collectionsThingsTable, ({ one }) => ({
collection: one(collectionsTable, {
fields: [collectionsThingsTable.collectionId],
references: [collectionsTable.id],
}),
item: one(itemsTable, {
fields: [collectionsThingsTable.itemId],
references: [itemsTable.id],
}),
})); Then I want to do three queries:
await db.query.itemsTable.findMany({
with: { collections: true },
});
await db.query.itemsTable.findMany({
with: { collections: false },
});
await db.query.itemsTable.findMany({
with: { collections: { where: eq(collectionsThingsTable.collectionId, collection) } },
}); Surprisingly all three snippets, just return all items. What's also interesting second snippet says that value can't be falsy. I can't understand why it is so. |
Beta Was this translation helpful? Give feedback.
Answered by
brachkow
Aug 4, 2024
Replies: 1 comment
-
After having a good sleep, I came to realization that https://orm.drizzle.team/docs/rqb#include-relations
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
brachkow
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
After having a good sleep, I came to realization that
with
just joins tables while I thought it will filter query by having or not having described relationship.https://orm.drizzle.team/docs/rqb#include-relations