volcanicislander
volcanicislander
Explore posts from servers
DTDrizzle Team
Created by volcanicislander on 10/4/2023 in #help
Inferred Types not picking up Relations
I'm having issues with my inferred types from the "Core Type API" not picking up relations. I am trying to model a 1-to-Many relationship between users and assignments . A user can "create many" assignments. My query looks like this:
await client(context.env.DB).query.assignmentsTable.findMany({
with: {
createdBy: true
},
});
await client(context.env.DB).query.assignmentsTable.findMany({
with: {
createdBy: true
},
});
And the involved tables and relations from my schema file are defined as:
export const assignmentsTable = sqliteTable("assignments", {
id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
position: text("position").notNull(),
// ...other columns
createdBy: integer("created_by")
.references(() => usersTable.id, { onDelete: "cascade" })
.notNull(),

export const assignmentsRelations = relations(
assignmentsTable,
({ one, many }) => ({
// 1 assignment is created by 1 user
createdBy: one(usersTable, {
fields: [assignmentsTable.createdBy],
references: [usersTable.id],
relationName: "assignments.created",
}),
}),
);

export const usersTable = sqliteTable("users", {
id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
userId: text("user_id").notNull(),
last: text("last").notNull(),
first: text("first").notNull(),
// ...other columns
});

export const usersRelations = relations(usersTable, ({ many }) => ({
assignmentsCreated: many(assignmentsTable, {
relationName: "assignments.created",
}),
// ...other relations
}));
export const assignmentsTable = sqliteTable("assignments", {
id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
position: text("position").notNull(),
// ...other columns
createdBy: integer("created_by")
.references(() => usersTable.id, { onDelete: "cascade" })
.notNull(),

export const assignmentsRelations = relations(
assignmentsTable,
({ one, many }) => ({
// 1 assignment is created by 1 user
createdBy: one(usersTable, {
fields: [assignmentsTable.createdBy],
references: [usersTable.id],
relationName: "assignments.created",
}),
}),
);

export const usersTable = sqliteTable("users", {
id: integer("id", { mode: "number" }).primaryKey({ autoIncrement: true }),
userId: text("user_id").notNull(),
last: text("last").notNull(),
first: text("first").notNull(),
// ...other columns
});

export const usersRelations = relations(usersTable, ({ many }) => ({
assignmentsCreated: many(assignmentsTable, {
relationName: "assignments.created",
}),
// ...other relations
}));
My type is defined as:
export type Assignment = typeof assignmentsTable.$inferSelect;
export type Assignment = typeof assignmentsTable.$inferSelect;
The query properly retrieves the user under the createdBy column but the typing still thinks the createdBy column should be a number and not the embedded user from the relation. What am I missing here? For reference, this is in a Remix project using Cloudflare D1.
9 replies