From f1a94ea94158e4c9ce1acae8d7ccfe67a5699ffe Mon Sep 17 00:00:00 2001 From: Amogh Bharadwaj <65964360+Amogh-Bharadwaj@users.noreply.github.com> Date: Mon, 4 Nov 2024 17:19:58 +0530 Subject: [PATCH] API/GetColumns: fix foreign keys being considered pkeys (#2213) Foreign keys that were unique columns and not pkeys had their column exclusion disabled in the UI as the current query to fetch column data was counting them as primary keys. This PR fixes this ### QA ```sql -- Create a projects table CREATE TABLE projects ( id SERIAL PRIMARY KEY, application_id INT ); -- Create the applications table CREATE TABLE applications ( id INTEGER UNIQUE ); -- Create foreign key relationship ALTER TABLE projects ADD CONSTRAINT fk_application FOREIGN KEY (application_id) REFERENCES applications (id); ``` Before checking out to this PR, id in the applications table will be disabled in UI, post checkout - will be non-disabled --- flow/cmd/peer_data.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/flow/cmd/peer_data.go b/flow/cmd/peer_data.go index 9faf61c39..cb625978f 100644 --- a/flow/cmd/peer_data.go +++ b/flow/cmd/peer_data.go @@ -297,19 +297,20 @@ func (h *FlowRequestHandler) GetColumns( defer peerConn.Close(ctx) rows, err := peerConn.Query(ctx, `SELECT - distinct attname AS column_name, - format_type(atttypid, atttypmod) AS data_type, - (attnum = ANY(conkey)) AS is_primary_key + DISTINCT attname AS column_name, + format_type(atttypid, atttypmod) AS data_type, + (pg_constraint.contype = 'p') AS is_primary_key FROM pg_attribute JOIN pg_class ON pg_attribute.attrelid = pg_class.oid - JOIN pg_namespace on pg_class.relnamespace = pg_namespace.oid + JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid LEFT JOIN pg_constraint ON pg_attribute.attrelid = pg_constraint.conrelid AND pg_attribute.attnum = ANY(pg_constraint.conkey) + AND pg_constraint.contype = 'p' WHERE pg_namespace.nspname = $1 AND relname = $2 AND pg_attribute.attnum > 0 AND NOT attisdropped - ORDER BY column_name`, req.SchemaName, req.TableName) + ORDER BY column_name;`, req.SchemaName, req.TableName) if err != nil { return nil, err }