From 50bdb6a3de9f68f406bbee55e13e711081e4f725 Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Sat, 13 Jul 2024 17:51:54 +0200 Subject: [PATCH] fix: Embed One-to-One relationship with different column order properly --- CHANGELOG.md | 1 + src/PostgREST/SchemaCache.hs | 2 +- test/spec/fixtures/schema.sql | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ebc94db17..b86b3ef0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Fixed - #3091, Broken link in OpenAPI description `externalDocs` - @salim-b + - #3659, Embed One-to-One relationship with different column order properly - @wolfgangwalther ### Changed diff --git a/src/PostgREST/SchemaCache.hs b/src/PostgREST/SchemaCache.hs index fda01d620..a21cdcb29 100644 --- a/src/PostgREST/SchemaCache.hs +++ b/src/PostgREST/SchemaCache.hs @@ -783,7 +783,7 @@ allM2OandO2ORels = JOIN LATERAL ( SELECT array_agg(row(cols.attname, refs.attname) order by ord) AS cols_and_fcols, - jsonb_agg(cols.attname order by ord) AS cols + jsonb_agg(cols.attname order by cols.attnum) AS cols FROM unnest(traint.conkey, traint.confkey) WITH ORDINALITY AS _(col, ref, ord) JOIN pg_attribute cols ON cols.attrelid = traint.conrelid AND cols.attnum = col JOIN pg_attribute refs ON refs.attrelid = traint.confrelid AND refs.attnum = ref diff --git a/test/spec/fixtures/schema.sql b/test/spec/fixtures/schema.sql index d20c36889..67cfd7f6f 100644 --- a/test/spec/fixtures/schema.sql +++ b/test/spec/fixtures/schema.sql @@ -2839,7 +2839,7 @@ CREATE TABLE test.students_info( , code text , address text , primary key(id, code) -, foreign key (id, code) references test.students(id, code) on delete cascade +, foreign key (code, id) references test.students(code, id) on delete cascade ); CREATE TABLE test.country(