16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/foreigner/connection_adapters/postgresql_adapter.rb', line 16
def foreign_keys(table_name)
fk_info = select_all %{
SELECT tc.constraint_name as name
,ccu.table_name as to_table
,ccu.column_name as primary_key
,kcu.column_name as column
,rc.delete_rule as dependency
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage kcu
USING (constraint_catalog, constraint_schema, constraint_name)
JOIN information_schema.referential_constraints rc
USING (constraint_catalog, constraint_schema, constraint_name)
JOIN information_schema.constraint_column_usage ccu
USING (constraint_catalog, constraint_schema, constraint_name)
WHERE tc.constraint_type = 'FOREIGN KEY'
AND tc.constraint_catalog = '#{@config[:database]}'
AND tc.table_name = '#{table_name}'
}
fk_info.map do |row|
options = {:column => row['column'], :name => row['name'], :primary_key => row['primary_key']}
options[:dependent] = case row['dependency']
when 'CASCADE' then :delete
when 'SET NULL' then :nullify
when 'RESTRICT' then :restrict
end
ForeignKeyDefinition.new(table_name, row['to_table'], options)
end
end
|