| Goal | This query identifies foreign key constraints that likely require an ON DELETE CASCADE action. It targets a specific pattern: where the foreign key constraint's columns are identical to a candidate key (Primary Key or Unique constraint) of the same table. This structure implements a one-to-one identifying relationship, where the child entity is existentially dependent on the parent (a weak entity depending on a strong entity). The absence of ON DELETE CASCADE in this scenario is a design flaw, as it prevents the parent row from being deleted and breaks the conceptual model. |
|---|---|
| Notes | In case of the string_agg function, the line break (br) tag is used as a part of the separator for the better readability in case the query result is displayed in a web browser. |
| Type | Problem detection (Each row in the result could represent a flaw in the design) |
| Reliability | Medium (Medium number of false-positive results) |
| License | MIT (opens in new tab) |
| Fixing Suggestion | Drop the foreign key constraint and recreate with ON DELETE CASCADE compensating action. |
| Data Source | system catalog only |
| SQL Query |
|
SQL statements that help generate fixes for the identified problem.
WITH fk AS (SELECT
o.conname,
(SELECT nspname FROM pg_namespace WHERE oid=c.relnamespace) AS target_schema,
c.relname AS target_table,
c.oid AS target_table_oid,
o.conkey
FROM pg_constraint o INNER JOIN pg_class c on c.oid = o.conrelid
INNER JOIN pg_class f on f.oid = o.confrelid
WHERE o.contype = 'f' and o.confdeltype<>'c'),
ck AS (SELECT
o.conname,
(SELECT nspname FROM pg_namespace WHERE oid=c.relnamespace) AS target_schema,
c.relname AS target_table,
o.conkey
FROM pg_constraint o INNER JOIN pg_class c on c.oid = o.conrelid
WHERE o.contype in ('p','u')),
one_to_one AS (SELECT fk.conname, fk.target_schema, fk.target_table, unnest(fk.conkey) AS target_column, fk.target_table_oid
FROM fk,ck
WHERE fk.target_schema=ck.target_schema AND fk.target_table=ck.target_table AND fk.conkey <@ ck.conkey AND fk.conkey @> ck.conkey)
SELECT format('ALTER TABLE %1$I.%2$I DROP CONSTRAIN %3$I;', target_schema, target_table, conname) AS statements
FROM one_to_one o INNER JOIN pg_attribute a on o.target_column = a.attnum and o.target_table_oid = a.attrelid
GROUP BY conname, target_schema, target_table
ORDER BY target_schema, target_table, conname;Drop the foreign key constraint.
| SQL Query to Generate Fix | Description |
|---|---|
| Drop the foreign key constraint. |
Collections
This query belongs to the following collections:
Find problems about integrity constraints
A selection of queries that return information about the state of integrity constraints in the datadabase. Contains all the types of queries - problem detection, software measure, and general overview
Find problems automatically
Queries, that results point to problems in the database. Each query in the collection produces an initial assessment. However, a human reviewer has the final say as to whether there is a problem or not .
| Name | Description |
|---|---|
| Find problems about integrity constraints | A selection of queries that return information about the state of integrity constraints in the datadabase. Contains all the types of queries - problem detection, software measure, and general overview |
| Find problems automatically | Queries, that results point to problems in the database. Each query in the collection produces an initial assessment. However, a human reviewer has the final say as to whether there is a problem or not . |
Categories
This query is classified under the following categories:
Comfortability of data management
Queries of this category provide information about the means that have been used to make the use or management of database more comfortable and thus, more efficient.
Compensating actions
Queries of this category provide information about compensating actions of foreign key constraints.
Relationships between tables
Queries of this category provide information about how database tables are connected to each other and whether such connections have been explicitly defined and whether it has been done correctly.
Validity and completeness
Queries of this category provide information about whether database design represents the world (domain) correctly (validity) and whether database design captures all the information about the world (domain) that is correct and relevant (completeness).
| Name | Description |
|---|---|
| Comfortability of data management | Queries of this category provide information about the means that have been used to make the use or management of database more comfortable and thus, more efficient. |
| Compensating actions | Queries of this category provide information about compensating actions of foreign key constraints. |
| Relationships between tables | Queries of this category provide information about how database tables are connected to each other and whether such connections have been explicitly defined and whether it has been done correctly. |
| Validity and completeness | Queries of this category provide information about whether database design represents the world (domain) correctly (validity) and whether database design captures all the information about the world (domain) that is correct and relevant (completeness). |