© 2026 Hedgehog Software, LLC

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

Cant query with relation using query method

Hi Guys

I facing a challenge where when i query and try to pull out the data from referenced table, it says TypeError: Cannot read properties of undefined (reading 'referencedTable')

here is my schema relations:
export const ratePlanRoomTypeRelation = relations(room_type, ({ many }) => ({
    rate_plans: many(rate_plans),
}))

export const ratePlanRoomTypeRelation2 = relations(rate_plans, ({ one }) => ({
    room_type: one(room_type, {
        fields: [rate_plans.room_type_id],
        references: [room_type.id]
    })
}))
export const ratePlanRoomTypeRelation = relations(room_type, ({ many }) => ({
    rate_plans: many(rate_plans),
}))

export const ratePlanRoomTypeRelation2 = relations(rate_plans, ({ one }) => ({
    room_type: one(room_type, {
        fields: [rate_plans.room_type_id],
        references: [room_type.id]
    })
}))

here is my query in sveltekit +page.server.ts
import type { PageServerLoad } from './$types.ts';
import * as schema from '$lib/schema.js';
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';
import { PUBLIC_DRIZZLE_DATABASE_URL } from '$env/static/public';
import { eq } from 'drizzle-orm';


const sql = postgres(PUBLIC_DRIZZLE_DATABASE_URL!);
const db = drizzle(sql, { schema: schema });

export const load = (async () => {
    const result = await db.query.room_type.findMany({
        with: {
            rate_plans: true,
            property: true
        },
    });
    
    return {
        data: {
            room_data: result,
        }
    }; // Return a plain object with the result
}) satisfies PageServerLoad;
import type { PageServerLoad } from './$types.ts';
import * as schema from '$lib/schema.js';
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';
import { PUBLIC_DRIZZLE_DATABASE_URL } from '$env/static/public';
import { eq } from 'drizzle-orm';


const sql = postgres(PUBLIC_DRIZZLE_DATABASE_URL!);
const db = drizzle(sql, { schema: schema });

export const load = (async () => {
    const result = await db.query.room_type.findMany({
        with: {
            rate_plans: true,
            property: true
        },
    });
    
    return {
        data: {
            room_data: result,
        }
    }; // Return a plain object with the result
}) satisfies PageServerLoad;


some facts

1. if i remove the with property:true, it's working but i can't get the data in property table
2. i try to double query and join it manually in the code, but not working because the 2nd query that query to the table separately is only able to filter it using single data, which i will have multiple property rows
image.png
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

db.query error with relation
Drizzle TeamDTDrizzle Team / help
3y ago
relation select query with where condition
Drizzle TeamDTDrizzle Team / help
3y ago
Using `query` with RLS.
Drizzle TeamDTDrizzle Team / help
12mo ago
Relation query `extras` needs access to `with`
Drizzle TeamDTDrizzle Team / help
3y ago