© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•14mo ago•
13 replies
Cody Oakes

Generated Postgres Column adding together other columns that may have null values

This code works until one of the numeric values is null, then the totalQuantity becomes null as well. Is there a way to do this in a drizzle schema that would account for the potential nulls?

const supplyItem = pgTable('supply_item', {
  id: bigint('id', { mode: 'number' }).primaryKey().generatedAlwaysAsIdentity(),
  name: text('name').notNull(),
  location1name: text('location1_name'),
  location1quantity: numeric('location1_quantity'),
  location2name: text('location2_name'),
  location2quantity: numeric('location2_quantity'),
  location3name: text('location3_name'),
  location3quantity: numeric('location3_quantity'),
  totalQuantity: numeric('total_quantity').generatedAlwaysAs(
    (): SQL =>
      sql`${supplyItem.location1quantity}+${supplyItem.location2quantity}+${supplyItem.location3quantity}`,
  ),
})
const supplyItem = pgTable('supply_item', {
  id: bigint('id', { mode: 'number' }).primaryKey().generatedAlwaysAsIdentity(),
  name: text('name').notNull(),
  location1name: text('location1_name'),
  location1quantity: numeric('location1_quantity'),
  location2name: text('location2_name'),
  location2quantity: numeric('location2_quantity'),
  location3name: text('location3_name'),
  location3quantity: numeric('location3_quantity'),
  totalQuantity: numeric('total_quantity').generatedAlwaysAs(
    (): SQL =>
      sql`${supplyItem.location1quantity}+${supplyItem.location2quantity}+${supplyItem.location3quantity}`,
  ),
})
Solution
Want to try something. In the
generated always as
generated always as
expression, remove the table name before each column name (
"supply_item"."location1_quantity"
"supply_item"."location1_quantity"
->
"location1_quantity"
"location1_quantity"
)
Jump to solution
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Postgres - Unique constraint that allows null
Drizzle TeamDTDrizzle Team / help
2y ago
Raw SQL / Postgres stored generated column in schema
Drizzle TeamDTDrizzle Team / help
3y ago
Generated Columns not working
Drizzle TeamDTDrizzle Team / help
2y ago
UpdatedAt column - Postgres
Drizzle TeamDTDrizzle Team / help
3y ago