I have a line_items table with following columns:
product_id
variant_id
variant_id is nullable.
Here is t
Another option is to use expressions in your key fields. This may not have been around when you asked the question, but could be helpful for others that come across this now.
CREATE UNIQUE INDEX line_items_prod_id_var_id_idx
ON line_items ( product_id, (coalesce(variant_id, 0)) );
Granted, this assumes that your variant_id is an auto-incrementing integer that started at 1. Also note the parentheses around the expression. Per the docs, they are required.
http://www.postgresql.org/docs/9.3/static/sql-createindex.html