Skip to content

Instantly share code, notes, and snippets.

@house9
Created December 2, 2011 15:00
Show Gist options
  • Save house9/1423533 to your computer and use it in GitHub Desktop.
Save house9/1423533 to your computer and use it in GitHub Desktop.
create_fk_script_postgres.sql
select
tables.table_name,
columns.column_name,
('make_fk_unless_exists :' || tables.table_name || ', :' || columns.column_name || ', :' || REPLACE(columns.column_name, '_id', 's')) as code
from information_schema.tables
inner join information_schema.columns
on information_schema.columns.table_name = information_schema.tables.table_name
where tables.table_schema = 'public'
and columns.column_name LIKE '%id'
and columns.column_name <> 'id'
and tables.table_type = 'BASE TABLE'
order by tables.table_name, columns.column_name;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment