© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•3y ago•
14 replies
Ꚃimon

Nullable relational query?

In the following, shouldn't
res.jobSeekerProfile
res.jobSeekerProfile
possibly be null? Basically what I want is to get the user - Include their jobSeekerProfile (if they have one). Currently, it seems to assume that jobSeekerProfile always exists for the user
let test = db.query.users.findFirst({
  where: (users, { eq }) => eq(users.id, '1'),
  with: {
    jobSeekerProfile: true,
  },
})

test.then((res) => {
  if (res) {
    res.jobSeekerProfile
  }
})
let test = db.query.users.findFirst({
  where: (users, { eq }) => eq(users.id, '1'),
  with: {
    jobSeekerProfile: true,
  },
})

test.then((res) => {
  if (res) {
    res.jobSeekerProfile
  }
})


It's telling me it's this type:
res: {
    id: string;
    name: string | null;
    email: string;
    emailVerified: Date | null;
    image: string | null;
    createdAt: Date;
    updatedAt: Date;
    jobSeekerProfile: {
        id: string;
        userId: string;
        bio: string | null;
        cvUrl: string | null;
    };
}
res: {
    id: string;
    name: string | null;
    email: string;
    emailVerified: Date | null;
    image: string | null;
    createdAt: Date;
    updatedAt: Date;
    jobSeekerProfile: {
        id: string;
        userId: string;
        bio: string | null;
        cvUrl: string | null;
    };
}


Shouldn't jobSeekerProfile be nullable?

This is the schema:
export const users = pgTable('users', {
  id: text('id').notNull().primaryKey(),
  name: text('name'),
  email: text('email').notNull(),
  emailVerified: timestamp('emailVerified', { mode: 'date' }),
  image: text('image'),
  createdAt: timestamp('createdAt', { mode: 'date' }).notNull(),
  updatedAt: timestamp('updatedAt', { mode: 'date' }).notNull(),
})

export const userRelations = relations(users, ({ one }) => ({
  jobSeekerProfile: one(jobSeekerProfiles, {
    fields: [users.id],
    references: [jobSeekerProfiles.userId],
  }),
}))

export const jobSeekerProfiles = pgTable('jobSeekerProfiles', {
    id: text('id').notNull().primaryKey(),
    userId: text('userId')
      .notNull()
      .references(() => users.id, { onDelete: 'cascade' }),
    bio: text('bio'),
    cvUrl: text('cvUrl'),
  })
export const users = pgTable('users', {
  id: text('id').notNull().primaryKey(),
  name: text('name'),
  email: text('email').notNull(),
  emailVerified: timestamp('emailVerified', { mode: 'date' }),
  image: text('image'),
  createdAt: timestamp('createdAt', { mode: 'date' }).notNull(),
  updatedAt: timestamp('updatedAt', { mode: 'date' }).notNull(),
})

export const userRelations = relations(users, ({ one }) => ({
  jobSeekerProfile: one(jobSeekerProfiles, {
    fields: [users.id],
    references: [jobSeekerProfiles.userId],
  }),
}))

export const jobSeekerProfiles = pgTable('jobSeekerProfiles', {
    id: text('id').notNull().primaryKey(),
    userId: text('userId')
      .notNull()
      .references(() => users.id, { onDelete: 'cascade' }),
    bio: text('bio'),
    cvUrl: text('cvUrl'),
  })
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

Relational query problem
Drizzle TeamDTDrizzle Team / help
3y ago
[Relational Queries] Nullable one-to-one relationship
Drizzle TeamDTDrizzle Team / help
3y ago
advanced relational query builder
Drizzle TeamDTDrizzle Team / help
3y ago
Drizzle relational query, filters
Drizzle TeamDTDrizzle Team / help
3y ago