Fully qualify table columns in query object

This fixes the error UK's is experiencing:

```
PG::AmbiguousColumn: ERROR: column reference "state" is ambiguous LINE
1: SELECT DISTINCT spree_orders.*, CASE WHEN state IN ('cancele...
^ : SELECT DISTINCT spree_orders.*, CASE WHEN state IN ('canceled',
'returned') THEN payment_total WHEN state IS NOT NULL THEN payment_total
- total ELSE 0 END AS balance_value, spree_orders.* FROM "spree_orders"
INNER JOIN "spree_shipments"
```

See
https://app.bugsnag.com/yaycode/openfoodnetwork-uk/errors/6058c45989d37300079e8312?event_id=6058ccd30075af73bcb20000&i=sk&m=nw.
This commit is contained in:
Pau Perez
2021-03-22 18:59:48 +01:00
parent ec4bae3995
commit 199d48123b
2 changed files with 4 additions and 4 deletions

View File

@@ -30,8 +30,8 @@ class OutstandingBalance
# a little longer. See https://github.com/rails/arel/pull/400 for details.
def statement
<<-SQL.strip_heredoc
CASE WHEN state IN #{non_fulfilled_states_group.to_sql} THEN payment_total
WHEN state IS NOT NULL THEN payment_total - total
CASE WHEN "spree_orders"."state" IN #{non_fulfilled_states_group.to_sql} THEN "spree_orders"."payment_total"
WHEN "spree_orders"."state" IS NOT NULL THEN "spree_orders"."payment_total" - "spree_orders"."total"
ELSE 0 END
SQL
end

View File

@@ -12,8 +12,8 @@ describe OutstandingBalance do
normalized_sql_statement = normalize(outstanding_balance.statement)
expect(normalized_sql_statement).to eq(normalize(<<-SQL))
CASE WHEN state IN ('canceled', 'returned') THEN payment_total
WHEN state IS NOT NULL THEN payment_total - total
CASE WHEN "spree_orders"."state" IN ('canceled', 'returned') THEN "spree_orders"."payment_total"
WHEN "spree_orders"."state" IS NOT NULL THEN "spree_orders"."payment_total" - "spree_orders"."total"
ELSE 0 END
SQL
end