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?
Solution:Jump to solution
Want to try something. In the
generated always as
expression, remove the table name before each column name ("supply_item"."location1_quantity"
-> "location1_quantity"
)8 Replies
@Cody Oakes You can use Postgres'
coalesce
function, which takes the last non-null value in a set that you provide:
@Mario564 Thanks for the quick reply! I attempted Postgres'
coalesce
function for the total_quantity field but if one of the supplyItem.locationXquantity values is null the total_quantity values ends up being null as well...am I missing something in the syntax?
Looks like it's written correctly
How does the generated migration look like after this change?
Solution
Want to try something. In the
generated always as
expression, remove the table name before each column name ("supply_item"."location1_quantity"
-> "location1_quantity"
)Nice that worked!
You think I can remove the table name from the schema file and drizzle will produce the migration that way? Also why doesn't it work with table names?
Hmm, looks like I get a type error if I try to change the schema file to :
You think I can remove the table name from the schema file and drizzle will produce the migration that way?You can try doing this: Not as elegant but it does (or at least should) work.
Also why doesn't it work with table names?Not entirely sure, apparently it's something to do with how Postgres handles references, scopes or something like that
gotcha ya with the
coalesce
ended up looking like
@Mario564 thank you for your help!