© 2026 Hedgehog Software, LLC

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

[Relational Queries] Nullable one-to-one relationship

Hey y'all.

I'm using the relational queries feature and have a one-to-one relationship between these two tables.
export const teamsT = mysqlTable("Teams", {
  id: varchar("id", { length: 21 }).primaryKey(),
  name: varchar("name", { length: 40 }).notNull(),
  joinCode: varchar("joinCode", { length: 8 }).notNull(),
});

export const projectsT = mysqlTable("Projects", {
  id: varchar("id", { length: 21 }).primaryKey(),
  teamId: varchar("teamId", { length: 21 }).notNull(),
  name: varchar("name", { length: 40 }).notNull(),
  description: varchar("description", { length: 255 }).notNull(),
  content: text("content").notNull(),
  submitted: boolean("submitted").notNull(),
  submittedTime: timestamp("submittedTime"),
});

export const teamsRelations = relations(teamsT, ({ one, many }) => ({
  project: one(projectsT, {
    fields: [teamsT.id],
    references: [projectsT.teamId],
  }),
}));

export const projectsRelations = relations(projectsT, ({ one, many }) => ({
  team: one(teamsT, {
    fields: [projectsT.teamId],
    references: [teamsT.id],
  }),
}));
export const teamsT = mysqlTable("Teams", {
  id: varchar("id", { length: 21 }).primaryKey(),
  name: varchar("name", { length: 40 }).notNull(),
  joinCode: varchar("joinCode", { length: 8 }).notNull(),
});

export const projectsT = mysqlTable("Projects", {
  id: varchar("id", { length: 21 }).primaryKey(),
  teamId: varchar("teamId", { length: 21 }).notNull(),
  name: varchar("name", { length: 40 }).notNull(),
  description: varchar("description", { length: 255 }).notNull(),
  content: text("content").notNull(),
  submitted: boolean("submitted").notNull(),
  submittedTime: timestamp("submittedTime"),
});

export const teamsRelations = relations(teamsT, ({ one, many }) => ({
  project: one(projectsT, {
    fields: [teamsT.id],
    references: [projectsT.teamId],
  }),
}));

export const projectsRelations = relations(projectsT, ({ one, many }) => ({
  team: one(teamsT, {
    fields: [projectsT.teamId],
    references: [teamsT.id],
  }),
}));


I have this query to fetch a user, their team (if they have one) and their team's project (if they have one).
const result = await db.query.registrationsT.findFirst({
    where: eq(registrationsT.userId, ctx.user.id),
    columns: {
      id: true,
    },
    with: {
      team: {
        columns: {
          id: true,
          name: true,
        },
        with: {
          project: {
            columns: {
              id: true,
            },
          },
        },
      },
    },
  });
const result = await db.query.registrationsT.findFirst({
    where: eq(registrationsT.userId, ctx.user.id),
    columns: {
      id: true,
    },
    with: {
      team: {
        columns: {
          id: true,
          name: true,
        },
        with: {
          project: {
            columns: {
              id: true,
            },
          },
        },
      },
    },
  });


This is the return type of that function.
{
  id: number;
  team: { id: string; name: string; project: { id: string } } | null;
} | undefined
{
  id: number;
  team: { id: string; name: string; project: { id: string } } | null;
} | undefined


The problem is, project could potentially be
null
null
if the team does not have a project.
I'm not really sure what to do here to tell TS that project can be
null
null
. Am I creating the relations correctly or am I doing something wrong? How should I solve this?

Thank you in advance for the help and I apologize for the long question.
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

Nullable relational query?
Drizzle TeamDTDrizzle Team / help
3y ago
Conditional Relational Queries
Drizzle TeamDTDrizzle Team / help
3y ago
Relational queries (PlanetScale)
Drizzle TeamDTDrizzle Team / help
3y ago
Relational queries: many-to-many
Drizzle TeamDTDrizzle Team / help
3y ago