The list of all the queries

The expression of a simple check constraint that is associated directly with a column needs type conversion

Query goal: Find check constraints that involve one column and are associated directly with a table where the Boolean expression invokes an operation that does not match with the data type of the column.
Notes about the query: The query considers only check constraints that are associated with one column.
Query type: Problem detection (Each row in the result could represent a flaw in the design)
Query reliability: Medium (Medium number of false-positive results)
Query license: MIT License
Fixing suggestion: Perhaps the Boolean expression is incorrect. For instance, CHECK(price::text<>''). In this case drop the constraint and think through what you have to check, whether the check is implemented already with the selection of the data type, and if not then what would be a correct Boolean expression. It could also be that the data type of the column is incorrect and has to be changed.
Data source: INFORMATION_SCHEMA+system catalog
SQL query: Click on query to copy it

WITH simple_checks AS (SELECT 
(select nspname from pg_namespace where oid=c.relnamespace) as table_schema,
c.relname AS table_name,
o.conname AS constraint_name
FROM pg_constraint o INNER JOIN pg_class c ON c.oid = o.conrelid
WHERE o.contype = 'c' AND cardinality(o.conkey)=1),
checks AS (SELECT ccu.table_schema, ccu.table_name, c.column_name, c.ordinal_position, c.data_type, cc.constraint_name, cc.check_clause
FROM INFORMATION_SCHEMA.constraint_column_usage AS ccu INNER JOIN INFORMATION_SCHEMA.check_constraints AS cc USING (constraint_schema, constraint_name)
INNER JOIN INFORMATION_SCHEMA.columns AS c USING (table_schema, table_name, column_name)
WHERE cc.check_clause NOT LIKE '%IS NOT NULL' AND 
ccu.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 (ccu.table_schema, ccu.table_name, cc.constraint_name) IN (SELECT table_schema, table_name, constraint_name
FROM simple_checks))
SELECT table_schema, table_name, column_name, data_type, constraint_name, check_clause
FROM checks
WHERE (data_type !~* '^(character|text)' AND check_clause ~*'::(bpchar|character|text).*(=|>|<|~).*::(bpchar|character|text)' AND check_clause !~*'(to_date|to_timestamp)[(][^)]*::(bpchar|character|text)')
OR
(data_type ~* '^(character|text)' AND check_clause !~*'::(bpchar|character|text)' AND check_clause ~*'::')
ORDER BY table_schema, table_name, ordinal_position;

SQL statements for generating SQL statements that help us to fix the problem

SQL queryDescription
WITH simple_checks AS (SELECT 
(select nspname from pg_namespace where oid=c.relnamespace) as table_schema,
c.relname AS table_name,
o.conname AS constraint_name
FROM pg_constraint o INNER JOIN pg_class c ON c.oid = o.conrelid
WHERE o.contype = 'c' AND cardinality(o.conkey)=1),
checks AS (SELECT ccu.table_schema, ccu.table_name, c.data_type, cc.constraint_name, cc.check_clause
FROM INFORMATION_SCHEMA.constraint_column_usage AS ccu INNER JOIN INFORMATION_SCHEMA.check_constraints AS cc USING (constraint_schema, constraint_name)
INNER JOIN INFORMATION_SCHEMA.columns AS c USING (table_schema, table_name, column_name)
WHERE cc.check_clause NOT LIKE '%IS NOT NULL' AND 
ccu.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 (ccu.table_schema, ccu.table_name, cc.constraint_name) IN (SELECT table_schema, table_name, constraint_name
FROM simple_checks))
SELECT format('ALTER TABLE %1$I.%2$I DROP CONSTRAINT %3$I;', table_schema, table_name, constraint_name) AS statements
FROM checks
WHERE (data_type !~* '^(character|text)' AND check_clause ~*'::(bpchar|character|text).*(=|>|<|~).*::(bpchar|character|text)' AND check_clause !~*'(to_date|to_timestamp)[(][^)]*::(bpchar|character|text)')
OR
(data_type ~* '^(character|text)' AND check_clause !~*'::(bpchar|character|text)' AND check_clause ~*'::')
ORDER BY table_schema, table_name, constraint_name;
Drop the constraint.

Collections where the query belongs to

Collection nameCollection description
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 where the query belongs to

Category nameCategory description
CHECK constraintsQueries of this category provide information about CHECK constraints.
Data typesQueries of this category provide information about the data types and their usage.
Validity and completenessQueries 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).

The list of all the queries