Created
December 2, 2011 15:00
-
-
Save house9/1423533 to your computer and use it in GitHub Desktop.
create_fk_script_postgres.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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