Skip to content

Commit

Permalink
API/GetColumns: fix foreign keys being considered pkeys (#2213)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Amogh-Bharadwaj authored Nov 4, 2024
1 parent 93ec020 commit f1a94ea
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions flow/cmd/peer_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit f1a94ea

Please sign in to comment.