© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•3y ago•
8 replies
volcanicislander

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
users
and
assignments
assignments
. A
user
user
can "create many"
assignments
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
user
under the
createdBy
createdBy
column but the typing still thinks the
createdBy
createdBy
column should be a number and not the embedded
user
user
from the relation. What am I missing here?

For reference, this is in a Remix project using Cloudflare D1.
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

Explicit inferred types
Drizzle TeamDTDrizzle Team / help
3y ago
help with drizzle types not inferred well
Drizzle TeamDTDrizzle Team / help
3y ago
Types aren't being inferred properly
Drizzle TeamDTDrizzle Team / help
17mo ago
Invalid types with recursive relations
Drizzle TeamDTDrizzle Team / help
2w ago