Skip to content

Commit dbab4cb

Browse files
committed
fix: load any foreign keys, anyway if the user owns them, on PostgreSQL
Refs #1653
1 parent 3e0bf91 commit dbab4cb

File tree

1 file changed

+50
-31
lines changed

1 file changed

+50
-31
lines changed

source/dbconnection.pas

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6544,37 +6544,56 @@ function TPgConnection.GetTableForeignKeys(Table: TDBObject): TForeignKeyList;
65446544
// see #158
65456545
Result := TForeignKeyList.Create(True);
65466546
try
6547-
ForeignQuery := GetResults('SELECT'+
6548-
' refc.constraint_name,'+
6549-
' refc.update_rule,'+
6550-
' refc.delete_rule,'+
6551-
' kcu.table_name,'+
6552-
' STRING_AGG(distinct kcu.column_name, '','') AS columns,'+
6553-
' ccu.table_schema AS ref_schema,'+
6554-
' ccu.table_name AS ref_table,'+
6555-
' STRING_AGG(distinct ccu.column_name, '','') AS ref_columns,'+
6556-
' STRING_AGG(distinct kcu.ordinal_position::text, '','') AS ord_position'+
6557-
' FROM'+
6558-
' '+InfSch+'.referential_constraints AS refc,'+
6559-
' '+InfSch+'.key_column_usage AS kcu,'+
6560-
' '+InfSch+'.constraint_column_usage AS ccu'+
6561-
' WHERE'+
6562-
' refc.constraint_schema = '+EscapeString(Table.Schema)+
6563-
' AND kcu.table_name = '+EscapeString(Table.Name)+
6564-
' AND kcu.constraint_name = refc.constraint_name'+
6565-
' AND kcu.table_schema = refc.constraint_schema'+
6566-
' AND ccu.constraint_name = refc.constraint_name'+
6567-
' AND ccu.constraint_schema = refc.constraint_schema'+
6568-
' GROUP BY'+
6569-
' refc.constraint_name,'+
6570-
' refc.update_rule,'+
6571-
' refc.delete_rule,'+
6572-
' kcu.table_name,'+
6573-
' ccu.table_schema,'+
6574-
' ccu.table_name'+
6575-
' ORDER BY'+
6576-
' ord_position'
6577-
);
6547+
ForeignQuery := GetResults(
6548+
'SELECT ' +
6549+
' con.conname AS constraint_name, ' +
6550+
' CASE con.confupdtype ' +
6551+
' WHEN ''a'' THEN ''NO ACTION'' ' +
6552+
' WHEN ''r'' THEN ''RESTRICT'' ' +
6553+
' WHEN ''c'' THEN ''CASCADE'' ' +
6554+
' WHEN ''n'' THEN ''SET NULL'' ' +
6555+
' WHEN ''d'' THEN ''SET DEFAULT'' ' +
6556+
' END AS update_rule, ' +
6557+
' CASE con.confdeltype ' +
6558+
' WHEN ''a'' THEN ''NO ACTION'' ' +
6559+
' WHEN ''r'' THEN ''RESTRICT'' ' +
6560+
' WHEN ''c'' THEN ''CASCADE'' ' +
6561+
' WHEN ''n'' THEN ''SET NULL'' ' +
6562+
' WHEN ''d'' THEN ''SET DEFAULT'' ' +
6563+
' END AS delete_rule, ' +
6564+
' src_ns.nspname AS table_schema, ' +
6565+
' src_tbl.relname AS table_name, ' +
6566+
' string_agg(src_col.attname, '','' ORDER BY ord.pos) AS columns, ' +
6567+
' ref_ns.nspname AS ref_schema, ' +
6568+
' ref_tbl.relname AS ref_table, ' +
6569+
' string_agg(ref_col.attname, '','' ORDER BY ord.pos) AS ref_columns, ' +
6570+
' string_agg(ord.pos::text, '','' ORDER BY ord.pos) AS ord_position ' +
6571+
'FROM pg_constraint con ' +
6572+
'JOIN pg_class src_tbl ON src_tbl.oid = con.conrelid ' +
6573+
'JOIN pg_namespace src_ns ON src_ns.oid = src_tbl.relnamespace ' +
6574+
'JOIN LATERAL unnest(con.conkey) WITH ORDINALITY AS ord(attnum, pos) ON TRUE ' +
6575+
'JOIN pg_attribute src_col ON src_col.attrelid = src_tbl.oid AND src_col.attnum = ord.attnum ' +
6576+
'JOIN pg_class ref_tbl ON ref_tbl.oid = con.confrelid ' +
6577+
'JOIN pg_namespace ref_ns ON ref_ns.oid = ref_tbl.relnamespace ' +
6578+
'JOIN LATERAL unnest(con.confkey) WITH ORDINALITY AS ref_ord(attnum, pos) ' +
6579+
' ON ref_ord.pos = ord.pos ' +
6580+
'JOIN pg_attribute ref_col ON ref_col.attrelid = ref_tbl.oid AND ref_col.attnum = ref_ord.attnum ' +
6581+
'WHERE ' +
6582+
' con.contype = ''f'' ' +
6583+
' AND src_ns.nspname = '+EscapeString(Table.Schema) +
6584+
' AND src_tbl.relname = '+EscapeString(Table.Name) +
6585+
'GROUP BY ' +
6586+
' con.conname, ' +
6587+
' con.confupdtype, ' +
6588+
' con.confdeltype, ' +
6589+
' src_ns.nspname, ' +
6590+
' src_tbl.relname, ' +
6591+
' ref_ns.nspname, ' +
6592+
' ref_tbl.relname ' +
6593+
'ORDER BY ' +
6594+
' MIN(ord.pos)'
6595+
);
6596+
65786597
while not ForeignQuery.Eof do begin
65796598
ForeignKey := TForeignKey.Create(Self);
65806599
Result.Add(ForeignKey);

0 commit comments

Comments
 (0)