Error loading books: TypeError: Cannot read properties of undefined (reading 'referencedTable')

I searched and found a similar github issue that mentioned to fix this, I have to relations generated by drizzle, but I can't find any relations automatically being generated.
No description
3 Replies
quantumleaps
quantumleapsOP2mo ago
sample schema :
export const book = sqliteTable("books", {
id: text("id").primaryKey(),
title: text('title').notNull(),
description: text('description'),
isbn: text('isbn'),
authors: text('authors').notNull(),
language: text('language').notNull().default('english'),
createdAt: integer('created_at', { mode: 'timestamp' }).notNull(),
updatedAt: integer('updated_at', { mode: 'timestamp' }).notNull(),
});

export const bookImages = sqliteTable("book_images", {
id: text("id").primaryKey(),
bookId: text('book_id').notNull().references(() => book.id),
url: text('url').notNull(),
order: integer('order').notNull().default(0),
createdAt: integer('created_at', { mode: 'timestamp' }).notNull(),
updatedAt: integer('updated_at', { mode: 'timestamp' }).notNull(),
});

export const bookFiles = sqliteTable("book_files", {
id: text("id").primaryKey(),
bookId: text('book_id').notNull().references(() => book.id),
downloadUrl: text('download_url').notNull(),
fileType: text('file_type').notNull(),
fileSize: text('file_size').notNull(),
createdAt: integer('created_at', { mode: 'timestamp' }).notNull(),
updatedAt: integer('updated_at', { mode: 'timestamp' }).notNull(),
});

// Pricing & Inventory
export const pricing = sqliteTable('pricing', {
id: text('id').primaryKey(),
bookId: text('book_id').references(() => book.id),
price: real('price').notNull(),
currency: text('currency').notNull().default('USD'),
createdAt: integer('created_at', { mode: 'timestamp' }).notNull(),
updatedAt: integer('updated_at', { mode: 'timestamp' }).notNull(),
});
export const book = sqliteTable("books", {
id: text("id").primaryKey(),
title: text('title').notNull(),
description: text('description'),
isbn: text('isbn'),
authors: text('authors').notNull(),
language: text('language').notNull().default('english'),
createdAt: integer('created_at', { mode: 'timestamp' }).notNull(),
updatedAt: integer('updated_at', { mode: 'timestamp' }).notNull(),
});

export const bookImages = sqliteTable("book_images", {
id: text("id").primaryKey(),
bookId: text('book_id').notNull().references(() => book.id),
url: text('url').notNull(),
order: integer('order').notNull().default(0),
createdAt: integer('created_at', { mode: 'timestamp' }).notNull(),
updatedAt: integer('updated_at', { mode: 'timestamp' }).notNull(),
});

export const bookFiles = sqliteTable("book_files", {
id: text("id").primaryKey(),
bookId: text('book_id').notNull().references(() => book.id),
downloadUrl: text('download_url').notNull(),
fileType: text('file_type').notNull(),
fileSize: text('file_size').notNull(),
createdAt: integer('created_at', { mode: 'timestamp' }).notNull(),
updatedAt: integer('updated_at', { mode: 'timestamp' }).notNull(),
});

// Pricing & Inventory
export const pricing = sqliteTable('pricing', {
id: text('id').primaryKey(),
bookId: text('book_id').references(() => book.id),
price: real('price').notNull(),
currency: text('currency').notNull().default('USD'),
createdAt: integer('created_at', { mode: 'timestamp' }).notNull(),
updatedAt: integer('updated_at', { mode: 'timestamp' }).notNull(),
});
sample code :
const books = await db.query.book.findMany({
with: {
images: true,
files: true,
pricing: true,

}) ?? [];
const books = await db.query.book.findMany({
with: {
images: true,
files: true,
pricing: true,

}) ?? [];
do I need to define relations manually? bump
TOSL
TOSL2mo ago
Drizzle ORM - Relations
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
TOSL
TOSL2mo ago
You don't need to use the Relations API if you don't want to. Simply create proper FKs and structuring your schema properly is enough to use the SQL-like select API. If you don't create the relations, you can't use the query API.

Did you find this page helpful?