Goal Find names of domain CHECK constraints that contain the name of the base that column the domain specifies. Domains are reusable artifacts that one should be able to use in case of defining different base tables. Thus, it is inappropriate to use the name of a particular table in the name of a domain constraint.
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 License
Data Source INFORMATION_SCHEMA only
SQL Query
SELECT cdu.table_schema, cdu.table_name, t.table_type, cdu.column_name, cc.check_clause, cc.constraint_name, dc.domain_schema, dc.domain_name
FROM INFORMATION_SCHEMA.column_domain_usage AS cdu INNER JOIN INFORMATION_SCHEMA.tables AS t USING (table_schema, table_name)
INNER JOIN INFORMATION_SCHEMA.domain_constraints AS dc USING (domain_schema, domain_name)
INNER JOIN INFORMATION_SCHEMA.check_constraints AS cc USING (constraint_schema, constraint_name)
WHERE t.table_type='BASE TABLE' 
AND cc.check_clause NOT LIKE '%IS NOT NULL'  
AND cdu.table_schema NOT IN (SELECT schema_name
FROM INFORMATION_SCHEMA.schemata
WHERE schema_name<>'public' AND
schema_owner='postgres' AND schema_name IS NOT NULL)
AND cc.constraint_name ILIKE '%' || cdu.table_name || '%'
ORDER BY dc.domain_schema, dc.domain_name;

Collections

This query belongs to the following collections:

NameDescription
Find problems automaticallyQueries, 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:

NameDescription
CHECK constraintsQueries of this category provide information about CHECK constraints.
DomainsQueries of this category provide information about reusable specifications of column properties.

Further reading and related materials:

Reference
The corresponding code smell in case of cleaning code is "N2: Choose Names at the Appropriate Level of Abstraction". (Robert C. Martin, Clean Code)