The list of all the queries

Inconsistent use of plural and singular in table names in the context of a relationship (English version)

Query goal: Find foreign key constraints in case of which the name of one of the tables is in plural and the name of another table is in singular.
Notes about the query: The query works somewhat correctly only if the names are in English. The query is based on a simplified assumption that in case of many words in English (of course, not all) the plural form is indicated by the letter "s" at the end of the word. If the name or its subcomponent ends with s and it is not preceded with an undersore and a single letter or a letter "s", then it is considered plural. Thus, "process" and "task_fs" are considered singular whereas "processes" and "clear_facts" are considered plural.
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
Data source: INFORMATION_SCHEMA+system catalog
SQL query: Click on query to copy it

WITH plural_singular AS (SELECT nspname AS table_schema, relname AS table_name, 
CASE WHEN regexp_replace(relname,'(stats_|stats$|status$|state$|alias|address|pays$|group$|lock$|sys|class$)','','g') ~* '(?<!((^|_)[[:alnum:]]{0,2}|(^|_)[bcdfghjklmnpqrsztvwxz]{0,4}|s))s(_|$)' 
THEN 'Perhaps plural' ELSE 'Perhaps singular' END AS comment_about_the_table_name
FROM pg_class INNER JOIN pg_namespace ON pg_class.relnamespace=pg_namespace.oid
WHERE relkind='r'
AND nspname NOT IN (SELECT schema_name
FROM INFORMATION_SCHEMA.schemata
WHERE schema_name<>'public' AND
schema_owner='postgres' AND schema_name IS NOT NULL)),
fk as (select
o.conname,
(select nspname from pg_namespace where oid=f.relnamespace) as foreign_schema,
f.relname as foreign_table,
f.oid as foreign_table_oid,
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,
o.conkey AS target_col
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'),
fk_unnest as (select conname, foreign_schema, foreign_table,  foreign_table_oid, foreign_col, foreign_col_num, target_schema, target_table, target_table_oid, target_col, target_col_num, ordin
from fk, unnest(fk.foreign_col, fk. target_col) with ordinality as f(foreign_col_num, target_col_num, ordin)),
fk_with_names as (select conname, foreign_schema, foreign_table, array_agg(a_foreign.attname order by ordin) as foreign_col, target_schema, target_table, array_agg(a_target.attname order by ordin) as target_col
from fk_unnest fk inner join pg_attribute a_foreign on fk.foreign_col_num = a_foreign.attnum and fk.foreign_table_oid = a_foreign.attrelid and a_foreign.attisdropped = false
inner join pg_attribute a_target on fk.target_col_num = a_target.attnum and fk.target_table_oid = a_target.attrelid and a_target.attisdropped = false
group by conname, foreign_schema, foreign_table, target_schema, target_table)
select conname, foreign_schema, foreign_table, foreign_col, psf.comment_about_the_table_name AS foreign_comment, 
target_schema, target_table, target_col, pst.comment_about_the_table_name AS target_comment
from fk_with_names AS fwn INNER JOIN plural_singular AS psf ON fwn.foreign_schema=psf.table_schema AND fwn.foreign_table=psf.table_name
INNER JOIN plural_singular AS pst ON fwn.target_schema=pst.table_schema AND fwn.target_table=pst.table_name
WHERE psf.comment_about_the_table_name<>pst.comment_about_the_table_name
order by target_schema, target_table, conname;

Collections where the query belongs to

Collection nameCollection description
Find problems about namesA selection of queries that return information about the names of database objects. Contains all the types of queries - problem detection, software measure, and general overview.

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.

The list of all the queries