错误:PG::FeatureNotSupported:错误:不能在 DEFAULT 表达式中使用列引用 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
以防万一您认为自己快要疯了(或者某些 googler 发现了这一点),问题是如果您使用
GENERATED
,Rails 会将 DEFAULT
列语句转换为 schema.rb
语句。为了让它正常运行,您需要切换到 structure.sql
方法。
也许您有另一个实际产生错误的迁移。你展示的那个没有:)