Relational queries result types are not working

Hi, When i call findMany or findFirst with no args, the result type is
const trips: {
[x: string]: unknown;
}[]
const trips: {
[x: string]: unknown;
}[]
When i include with arg, the result type do not provide the relational type query
const trips = await db.query.tripModel.findMany({
with: {
riderModel: true,
},
});
const trips = await db.query.tripModel.findMany({
with: {
riderModel: true,
},
});
result type
const trips: {
id: number;
companyId: number;
createdAt: Date;
lineId: number;
carNumber: number;
passengerCount: number;
kilometerTraveled: number;
averageSpeed: number;
finishedAt: Date;
}[]
const trips: {
id: number;
companyId: number;
createdAt: Date;
lineId: number;
carNumber: number;
passengerCount: number;
kilometerTraveled: number;
averageSpeed: number;
finishedAt: Date;
}[]
8 Replies
victormesquita.
victormesquita.OP15mo ago
am i missing something? i using monorepo and per-file schema
Noahh
Noahh15mo ago
How is your table and your relation defined?
victormesquita.
victormesquita.OP15mo ago
@Noahh Heres the tripModel: trip.ts
import { pgTable, integer, timestamp, serial, real, index } from 'drizzle-orm/pg-core';
import { relations } from 'drizzle-orm';
import { companyModel, lineModel, pathModel, riderModel } from '.';

export const tripModel = pgTable(
'trip',
{
id: serial('id').primaryKey(),
companyId: integer('companyId')
.notNull()
.references(() => companyModel.id),
lineId: integer('lineId')
.notNull()
.references(() => lineModel.id),
carNumber: integer('carNumber').notNull(),
passengerCount: integer('passengerCount').notNull().default(0),
kilometerTraveled: real('kilometerTraveled').notNull().default(0),
averageSpeed: real('kilometerTraveled').notNull().default(0),
createdAt: timestamp('createdAt', { withTimezone: true }).defaultNow(),
finishedAt: timestamp('finishedAt', { withTimezone: true }),
},
(table) => {
return {
lineIdIdx: index('lineIdIdx').on(table.lineId),
companyIdIdx: index('companyIdIdx').on(table.companyId),
carNumberIdx: index('carNumberIdx').on(table.carNumber),
};
},
);

export const tripModelRelations = relations(tripModel, ({ many, one }) => ({
paths: many(pathModel),
riders: many(riderModel),
line: one(lineModel),
}));

export type TripModel = typeof tripModel.$inferSelect;
export type NewTripModel = typeof tripModel.$inferInsert;
import { pgTable, integer, timestamp, serial, real, index } from 'drizzle-orm/pg-core';
import { relations } from 'drizzle-orm';
import { companyModel, lineModel, pathModel, riderModel } from '.';

export const tripModel = pgTable(
'trip',
{
id: serial('id').primaryKey(),
companyId: integer('companyId')
.notNull()
.references(() => companyModel.id),
lineId: integer('lineId')
.notNull()
.references(() => lineModel.id),
carNumber: integer('carNumber').notNull(),
passengerCount: integer('passengerCount').notNull().default(0),
kilometerTraveled: real('kilometerTraveled').notNull().default(0),
averageSpeed: real('kilometerTraveled').notNull().default(0),
createdAt: timestamp('createdAt', { withTimezone: true }).defaultNow(),
finishedAt: timestamp('finishedAt', { withTimezone: true }),
},
(table) => {
return {
lineIdIdx: index('lineIdIdx').on(table.lineId),
companyIdIdx: index('companyIdIdx').on(table.companyId),
carNumberIdx: index('carNumberIdx').on(table.carNumber),
};
},
);

export const tripModelRelations = relations(tripModel, ({ many, one }) => ({
paths: many(pathModel),
riders: many(riderModel),
line: one(lineModel),
}));

export type TripModel = typeof tripModel.$inferSelect;
export type NewTripModel = typeof tripModel.$inferInsert;
Noahh
Noahh15mo ago
your relation from trip->riderModel is called riders, try using that in with
Noahh
Noahh15mo ago
also, when you define relationships using one you should specify fields and references. I'd recommend reading the RQB docs a little more to improve the relations https://orm.drizzle.team/docs/rqb
victormesquita.
victormesquita.OP15mo ago
@Noahh Still not returning riders
victormesquita.
victormesquita.OP15mo ago
Oh, thank you @Noahh its now working, thank you
Noahh
Noahh15mo ago
Great!
Want results from more Discord servers?
Add your server