Error : PG::FeatureNotSupported: ERROR: cannot use column reference in DEFAULT expression LINE 1: ..._at" timestamp, "total" decimal DEFAULT (COALESCE(price, (0)...
class AddTotalToOrderLines < ActiveRecord::Migration[6.1]
def up
execute <<~SQL
ALTER TABLE order_lines
ADD COLUMN total numeric GENERATED ALWAYS AS (COALESCE(price, 0) *
COALESCE(quantity, 0)) STORED;
SQL
end
In case you thought you were going crazy (or some googler finds this), the issue is Rails converts a
GENERATED
column statement to aDEFAULT
one if you're usingschema.rb
. In order to get it to behave you need to switch over to thestructure.sql
approach.