I have a Postgresql schema that contains many tables
I set a rule where every foreign key column has its name starting with "fk_"
But I just noticed that for some of those fields, I forgot to set the foreign key constraints
Would you help me building a request that identifies all the fields starting with fk that do not have a foreign key constraint set ?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
You can use the following SQL query to identify columns that start with "fk_" but do not have a foreign key constraint:
sql
SELECT
table_name,
column_name
FROM
information_schema.columns
WHERE
column_name LIKE 'fk_%'
AND table_name IN (
SELECT
table_name
FROM
information_schema.tables
WHERE
table_schema = 'public' -- replace 'public' with your schema name if different
)
AND (table_name, column_name) NOT IN (
SELECT
table_name,
column_name
FROM
information_schema.referential_constraints
JOIN
information_schema.key_column_usage
ON referential_constraints.constraint_name = key_column_usage.constraint_name
WHERE
table_schema = 'public' -- replace 'public' with your schema name if different
);
However, the above query might not work as expected because information_schema.referential_constraints and information_schema.key_column_usage do not directly contain the column names.
A more accurate query would be:
sql
SELECT
c.table_name,
c.column_name
FROM
information_schema.columns c
WHERE
c.column_name LIKE 'fk_%'
AND c.table_name IN (
SELECT
table_name
FROM
information_schema.tables
WHERE
table_schema = 'public' -- replace 'public' with your schema name if different
)
AND c.table_name NOT IN (
SELECT
kcu.table_name
FROM
information_schema.key_column_usage kcu
JOIN
information_schema.referential_constraints rc
ON kcu.constraint_name = rc.constraint_name
WHERE
kcu.table_schema = 'public' -- replace 'public' with your schema name if different
AND kcu.column_name LIKE 'fk_%'
);
But this query still might not work as expected because it only checks if the table has any foreign key constraints, not if the specific column has a foreign key constraint.
A more accurate query would be:
sql
SELECT
c.table_name,
c.column_name
FROM
information_schema.columns c
WHERE
c.column_name LIKE 'fk_%'
AND c.table_name IN (
SELECT
table_name
FROM
information_schema.tables
WHERE
table_schema = 'public' -- replace 'public' with your schema name if different
)
AND (c.table_name, c.column_name) NOT IN (
SELECT
kcu.table_name,
kcu.column_name
FROM
information_schema.key_column_usage kcu
JOIN
information_schema.referential_constraints rc
ON kcu.constraint_name = rc.constraint_name
WHERE
kcu.table_schema = 'public' -- replace 'public' with your schema name if different
);
This query checks if the specific column has a foreign key constraint. If it does not, it will be included in the results.
Please note that you should replace 'public' with your schema name if it is different.
Also, keep in mind that these queries are case-sensitive. If your column names are in a different case, you might need to adjust the query accordingly.
It's also worth noting that these queries will only return columns that are in the information_schema.columns table, which means they will only return columns that are in tables that are in the information_schema.tables table. If you have tables that are not in the information_schema.tables table (for example, if they are in a different schema), they will not be included in the results.
You can use the pg_catalog tables instead of information_schema to get more accurate results:
sql
SELECT
c.relname AS table_name,
a.attname AS column_name
FROM
pg_class c
JOIN
pg_namespace n ON c.relnamespace = n.oid
JOIN
pg_attribute a ON c.oid = a.attrelid
WHERE
a.attname LIKE 'fk_%'
AND n.nspname = 'public' -- replace 'public' with your schema name if different
AND a.attnum > 0
AND (c.oid, a.attnum) NOT IN (
SELECT
conrelid,
confkey[1]
FROM
pg_constraint
WHERE
contype = 'f'
AND connamespace = (SELECT oid FROM pg_namespace WHERE nspname = 'public') -- replace 'public' with your schema name if different
);
This query uses the pg_catalog tables to get the list of columns that start with "fk_" and do not have a foreign key constraint. It should be more accurate than the previous queries.