| Goal | This query identifies foreign key constraints that are likely missing an ON UPDATE CASCADE action. It operates on the heuristic that foreign keys referencing a natural key should permit cascading updates. The query uses a naming convention to identify probable natural keys, specifically flagging foreign key columns having the suffix or prefix "code" on the assumption that such values are user-defined and may require modification. The absence of ON UPDATE CASCADE on these keys can lead to referential integrity violations when the parent key is updated. |
|---|---|
| Notes | The query considers both column names in English and Estonian. |
| 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 it with ON UPDATE CASCADE compensating action. Another possibility is that the names of key columns do not follow a naming convention and have to be changed. |
| Data Source | system catalog only |
| SQL Query |
|
SQL statements that help generate fixes for the identified problem.
SELECT DISTINCT format('ALTER TABLE %1$I.%2$I DROP CONSTRAIN %3$I;', target_schema, target_table, constraint_name) AS statements
from (select
(select nspname from pg_namespace where oid=m.relnamespace) as foreign_schema,
m.relname as foreign_table,
m.oid as foreign_table_oid,
unnest(o.confkey) AS foreign_col,
(select nspname from pg_namespace where oid=c.relnamespace) as target_schema,
c.relname as target_table,
c.oid as target_table_oid,
unnest(o.conkey) AS target_col,
o.confupdtype,
o.conname as constraint_name
from pg_constraint o inner join pg_class c on c.oid = o.conrelid
inner join pg_class m on m.oid = o.confrelid
where o.contype = 'f' and o.conrelid in (select oid from pg_class c where c.relkind = 'r')) f
inner join pg_attribute af on f.foreign_col = af.attnum and f.foreign_table_oid = af.attrelid and af.attisdropped = false
inner join pg_attribute at on f.target_col = at.attnum and f.target_table_oid = at.attrelid and at.attisdropped = false
AND (af.attname~*'(kood|code)$' OR af.attname~*'^(kood|code)_')
AND confupdtype<>'c'
ORDER BY statements;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.
Result quality depends on names
Queries of this category use names (for instance, column names) to try to guess the meaning of a database object. Thus, the goodness of names determines the number of false positive and false negative results.
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. |
| Result quality depends on names | Queries of this category use names (for instance, column names) to try to guess the meaning of a database object. Thus, the goodness of names determines the number of false positive and false negative results. |
| 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). |
Further reading and related materials: