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
No description
5 Replies
alka_99
alka_99OP9mo ago
it's not giving me type checking error, which is weird
No description
alka_99
alka_99OP9mo ago
Just realized that in the schema.ts im using roon_type for the name, and i should use property instead This is solved
IgnisMurasaki
IgnisMurasaki8mo ago
OP, sorry for necroing this post, but I'm also having trouble with the include relations query, what should be the property inside the with object ? my relation is declared like the following
export const eventsRelations = relations(events, ({ many, one }) => ({
host: one(users, {
fields: [events.createdBy],
references: [users.id],
}),
}));

export const usersRelations = relations(users, ({ many }) => ({
events: many(events),
}));
export const eventsRelations = relations(events, ({ many, one }) => ({
host: one(users, {
fields: [events.createdBy],
references: [users.id],
}),
}));

export const usersRelations = relations(users, ({ many }) => ({
events: many(events),
}));
and here's the query
async getEventBySlug(slug: string): Promise<selectEvent | undefined> {
const data = await db.query.events.findFirst({
with: { host: true },
where: and(eq(events.slug, slug), isNull(events.deletedAt)),
});

if (data) return data as selectEvent;
else return undefined;
}
async getEventBySlug(slug: string): Promise<selectEvent | undefined> {
const data = await db.query.events.findFirst({
with: { host: true },
where: and(eq(events.slug, slug), isNull(events.deletedAt)),
});

if (data) return data as selectEvent;
else return undefined;
}
i've also tried to change the with object to {users:true} but still receiving the referencedTable error
Mykhailo
Mykhailo8mo ago
Hello, @Vigne! What do you want to achieve with your query? As I see, your query is valid, you get many events and one related user to them (host).
IgnisMurasaki
IgnisMurasaki8mo ago
ah sorry, i forgot to informing about this, it turns out that I have to add the relations variables when creating initializing the drizzle it's been solved now
Want results from more Discord servers?
Add your server