You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd like to have is a warning that a foreign key constraint isn't indexed. Static analysis of the migration file can't provide enough information to eliminate false positives for such a rule, so querying a running database would be required.
Such a query might look like this:
WITH indexes AS (
SELECTn.nspnameas schema_name,
t.relnameas table_name,
a.attnameas column_name
FROM
pg_class t
JOIN
pg_index i ONt.oid=i.indrelidJOIN
pg_attribute a ONa.attrelid=t.oidANDa.attnum= ANY(i.indkey)
JOIN
pg_namespace n ONt.relnamespace=n.oidWHEREt.relkind='r'-- real tablesAND array_position(i.indkey, a.attnum) =0-- only first column of possibly compound index
),
foreign_keys AS (
SELECTn.nspnameAS schema_name,
cl.relnameAS table_name,
a.attnameAS column_name,
ct.connameAS constraint_name
FROM
pg_constraint ct
JOIN pg_class cl ONct.conrelid=cl.oidJOIN pg_namespace n ONcl.relnamespace=n.oidJOIN pg_attribute a ONa.attnum= ANY(ct.conkey) ANDa.attrelid=cl.oidWHEREct.contype='f'
)
SELECT
schema_name as"schema!",
table_name as"table!",
column_name as"name!"FROM foreign_keys
LEFT JOIN indexes USING (schema_name, table_name, column_name)
WHEREindexes.column_name IS NULLAND schema_name = ANY($1)
(query is not perfect but you get the idea)
The text was updated successfully, but these errors were encountered:
I'd like to have is a warning that a foreign key constraint isn't indexed. Static analysis of the migration file can't provide enough information to eliminate false positives for such a rule, so querying a running database would be required.
Such a query might look like this:
(query is not perfect but you get the idea)
The text was updated successfully, but these errors were encountered: