© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•13mo ago•
1 reply
terrxo

Type Inference Issue: Missing | null on Relational Fields in Drizzle ORM

When querying a company and joining related customer data, the resulting inferred TypeScript type for the customer property does not allow for null even though no record may exist. Because of this, the type inference is overly strict. In many cases, customer can be null, especially with a left join or if the relation is optional.
// Query 1: Drizzle relational query
// Result type incorrectly omits `| undefined` or `| null` for `customer`.
const res = db.query.company.findFirst({
  with: {
    customer: {
      columns: {
        companyId: true,
      },
    },
  },
});
// Inferred type of `res`: 
// PgRelationalQuery<{
//   name: string;
//   id: number;
//   website: string | null;
//   nameSearch: string | null;
//   websiteSearch: string | null;
//   createdAt: Date;
//   updatedAt: Date;
//   customer: {
//     companyId: number;
//   };
// } | undefined>  // <-- `customer` is not allowing null
// Query 1: Drizzle relational query
// Result type incorrectly omits `| undefined` or `| null` for `customer`.
const res = db.query.company.findFirst({
  with: {
    customer: {
      columns: {
        companyId: true,
      },
    },
  },
});
// Inferred type of `res`: 
// PgRelationalQuery<{
//   name: string;
//   id: number;
//   website: string | null;
//   nameSearch: string | null;
//   websiteSearch: string | null;
//   createdAt: Date;
//   updatedAt: Date;
//   customer: {
//     companyId: number;
//   };
// } | undefined>  // <-- `customer` is not allowing null

// Query 2: Equivalent raw select with left join
const test = await db
  .select()
  .from(company)
  .leftJoin(customer, eq(company.id, customer.companyId));

// Inferred type of `test`: 
// {
//   company: {
//     createdAt: Date;
//     updatedAt: Date;
//     id: number;
//     name: string;
//     website: string | null;
//     nameSearch: string | null;
//     websiteSearch: string | null;
//   };
//   customer: {
//     companyId: number;
//   } | null;
// }[]
//
// Here, `customer` is correctly typed to include `| null`.
// Query 2: Equivalent raw select with left join
const test = await db
  .select()
  .from(company)
  .leftJoin(customer, eq(company.id, customer.companyId));

// Inferred type of `test`: 
// {
//   company: {
//     createdAt: Date;
//     updatedAt: Date;
//     id: number;
//     name: string;
//     website: string | null;
//     nameSearch: string | null;
//     websiteSearch: string | null;
//   };
//   customer: {
//     companyId: number;
//   } | null;
// }[]
//
// Here, `customer` is correctly typed to include `| null`.

Expected Behavior:
When using a relational query (e.g. with: { customer: { ... } }), the inferred TypeScript type should include | null (or | undefined) for the customer property if it is optional or if no matching record exists.

Actual Behavior:
The inferred type forces the customer property to always exist, which causes runtime errors in bussines code because of the types mismatch.
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

Type inference breaking in drizzle-orm 0.32.0 ?
Drizzle TeamDTDrizzle Team / help
2y ago
Problem with relational drizzle ORM
Drizzle TeamDTDrizzle Team / help
15mo ago
Missing dependency on drizzle-orm in drizzle-kit?
Drizzle TeamDTDrizzle Team / help
2y ago
Drizzle ORM migrations on rename fields?
Drizzle TeamDTDrizzle Team / help
2y ago