The list of all the queries

Different suffixes of a candidate key column and a referencing foreign key column

Query goal: The naming must be consistent. Find foreign key constraints where the candidate key column and foreign key column names have different suffixes. Thus, for instance, one cannot use USING syntax for joining the tables.
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: Use the same suffix in case of the candidate key and foreign key column names. For instance, if the primary key column name is person_id, then the foreign key column should not have the name person_code and person_nr and vice versa.
Data source: system catalog only
SQL query: Click on query to copy it

WITH 
fk_constraint_names AS (select 
o.conname,
(select nspname from pg_namespace where oid=f.relnamespace) as foreign_ns,
f.relname as foreign_table,
(select a.attname from pg_attribute a where a.attrelid = f.oid and a.attnum = o.confkey[1] and a.attisdropped = false) as foreign_colname,
(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
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')
SELECT foreign_ns, foreign_table, foreign_colname, target_ns, target_table, target_colname
FROM fk_constraint_names
WHERE 
lower(regexp_replace(foreign_colname,'.*_([[:alnum:]]{1,4})$','\1'))<>lower(regexp_replace(target_colname,'.*_([[:alnum:]]{1,4})$','\1'))
AND lower(regexp_replace(foreign_colname,'.*_([[:alnum:]]{1,4})$','\1')) IN ('id','kood','code','nr')
AND lower(regexp_replace(target_colname,'.*_([[:alnum:]]{1,4})$','\1')) IN ('id','kood','code','nr')
ORDER BY target_ns, target_table;

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
InconsistenciesQueries of this catergory provide information about inconsistencies of solving the same problem in different places.
NamingQueries of this category provide information about the style of naming.
Relationships between tablesQueries 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.

Reference materials for further reading

Reference
The corresponding code smell in case of cleaning code is "G11: Inconsistency". (Robert C. Martin, Clean Code)

The list of all the queries