LarryHudson
LarryHudson
DTDrizzle Team
Created by LarryHudson on 11/16/2023 in #help
SQLite - one to many relationship, join only returning first match
// src/db/schema.ts
export const contentItems = sqliteTable("content_items", {
id: integer("id").primaryKey(),
title: text("title"),
});

export const contentItemRelations = relations(
contentItems,
({ one, many }) => ({
contentParts: many(contentParts),
}),
);

export const contentParts = sqliteTable("content_parts", {
id: integer("id").primaryKey(),
contentItemId: integer("content_item_id").references(() => contentItems.id),
title: text("title"),
});

export const contentPartRelations = relations(
contentParts,
({ one, many }) => ({
contentItem: one(contentItems, {
fields: [contentParts.contentItemId],
references: [contentItems.id],
}),
}),
);

// src/db/index.ts
import { drizzle } from "drizzle-orm/better-sqlite3";
import * as schema from "./schema";
import type { BetterSQLite3Database } from "drizzle-orm/better-sqlite3";
import Database from "better-sqlite3";

export const sqliteDatabase = new Database("./sqlite.db", {
verbose: console.log,
});
export const db: BetterSQLite3Database = drizzle(sqliteDatabase, { schema });

// src/content-items/index.ts
import { db } from "@src/db";
import {
contentItems as contentItemsTable,
} from "@src/db/schema";


export async function getContentItemById(contentItemId: number) {
const contentItem = await db.query.contentItems.findFirst({
where: (contentItemsTable, { eq }) => eq(contentItemsTable.id, contentItemId),
with: {
contentParts: true,
},
});
return contentItem;
}
// src/db/schema.ts
export const contentItems = sqliteTable("content_items", {
id: integer("id").primaryKey(),
title: text("title"),
});

export const contentItemRelations = relations(
contentItems,
({ one, many }) => ({
contentParts: many(contentParts),
}),
);

export const contentParts = sqliteTable("content_parts", {
id: integer("id").primaryKey(),
contentItemId: integer("content_item_id").references(() => contentItems.id),
title: text("title"),
});

export const contentPartRelations = relations(
contentParts,
({ one, many }) => ({
contentItem: one(contentItems, {
fields: [contentParts.contentItemId],
references: [contentItems.id],
}),
}),
);

// src/db/index.ts
import { drizzle } from "drizzle-orm/better-sqlite3";
import * as schema from "./schema";
import type { BetterSQLite3Database } from "drizzle-orm/better-sqlite3";
import Database from "better-sqlite3";

export const sqliteDatabase = new Database("./sqlite.db", {
verbose: console.log,
});
export const db: BetterSQLite3Database = drizzle(sqliteDatabase, { schema });

// src/content-items/index.ts
import { db } from "@src/db";
import {
contentItems as contentItemsTable,
} from "@src/db/schema";


export async function getContentItemById(contentItemId: number) {
const contentItem = await db.query.contentItems.findFirst({
where: (contentItemsTable, { eq }) => eq(contentItemsTable.id, contentItemId),
with: {
contentParts: true,
},
});
return contentItem;
}
19 replies
DTDrizzle Team
Created by LarryHudson on 11/16/2023 in #help
SQLite - one to many relationship, join only returning first match
I'll copy and paste in my code including the relations, in case anyone runs into the same issue
19 replies
DTDrizzle Team
Created by LarryHudson on 11/16/2023 in #help
SQLite - one to many relationship, join only returning first match
After a bit of head scratching I got the query builder working properly this morning! I was missing the part where you add the schema to the main DB export. Thanks again for your help @Angelelz 🙂
19 replies
DTDrizzle Team
Created by LarryHudson on 11/16/2023 in #help
SQLite - one to many relationship, join only returning first match
Ah ok, thanks for your help here! I might have a play with subqueries. I like the idea of staying a bit closer to SQL
19 replies
DTDrizzle Team
Created by LarryHudson on 11/16/2023 in #help
SQLite - one to many relationship, join only returning first match
Ah ok I see, thanks! I will look more into the relational query builder. Do you know if the query builder is compatible with SQLite and better-sqlite3?
19 replies
DTDrizzle Team
Created by LarryHudson on 11/16/2023 in #help
SQLite - one to many relationship, join only returning first match
Ah ok thanks for explaining that, so how would I get one content item, with all its content parts?
19 replies
DTDrizzle Team
Created by LarryHudson on 11/16/2023 in #help
SQLite - one to many relationship, join only returning first match
And just confirming, the relational query builder works with SQLite and better-sqlite3? I tried adding some relations and couldn’t get it to work, but I might need to spend more time playing with it
19 replies
DTDrizzle Team
Created by LarryHudson on 11/16/2023 in #help
SQLite - one to many relationship, join only returning first match
Ah ok, so SQL select doesn’t work the way I want it to?
19 replies