| Goal | Find foreign key constraints that use a SET NULL compensating action but a foreign key column is mandatory, i.e., does not permit NULLs. Compensatory actions cannot make changes that violate integrity constraints in a database. SET NULL cannot put NULL to a mandatory column (delete a foreign key value). |
|---|---|
| Notes | The query consider only simple foreign keys, i.e., foreign keys that involve only one column. |
| Type | Problem detection Each row in the result could represent a flaw in the design |
| Reliability | High Few or no false-positive results |
| License | MIT (opens in new tab) |
| Fixing Suggestion | Change the compensatory action (better) or make the foreign key columns optional (worse, due to potential problems that NULLs cause in formulating queries). In order to change the compensatory action one has to drop and recreate the foreign key constraint. |
| Data Source | INFORMATION_SCHEMA+system catalog |
| SQL Query |
|
SQL statements that help generate fixes for the identified problem.
WITH f AS (select
o.conname,
(select nspname from pg_namespace where oid=c.relnamespace) as target_ns,
c.relname as target_table,
(select a.attname from pg_attribute a where a.attrelid = c.oid and a.attnum = o.conkey[1] and a.attisdropped = false) as target_colname,
o.confupdtype, o.confdeltype
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 cardinality(o.conkey)=1)
SELECT format('ALTER TABLE %1$I.%2$I DROP CONSTRAINT %3$I;', target_ns, target_table, conname) AS statements
FROM f
WHERE (confupdtype='n' OR confdeltype='n') AND EXISTS (SELECT 1
FROM information_schema.columns AS c WHERE f.target_ns=c.table_schema AND f.target_table=c.table_name AND f.target_colname=c.column_name AND c.is_nullable='NO')
ORDER BY target_ns, target_table, target_colname;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 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 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:
Compensating actions
Queries of this category provide information about compensating actions of foreign key constraints.
Missing data
Queries of this category provide information about missing data (NULLs) in a database.
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.
| Name | Description |
|---|---|
| Compensating actions | Queries of this category provide information about compensating actions of foreign key constraints. |
| Missing data | Queries of this category provide information about missing data (NULLs) in a database. |
| 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. |