Use pg-specific schema tables for fkey detection
- Postgres doesn't care about names for uniqueness of keys unlike mysql because internally it keeps "oid" values to keep track of everything. Unfortunately this means that the information_schema standard is inadequate to differentiate between constraints that are named the same (which isn't possible in mysql, but is in pg). Hence we have to dip into the pg specific schemas for better or worse. - Fix naming of the sample schema in the README since it would fail for mysql due to duplicate naming. - Mark test schema up so we don't fix the bad names so we catch regressions here. - Fix #85
This commit is contained in:
parent
fac1a7fe69
commit
711ecbbe8d
3 changed files with 17 additions and 12 deletions
bdb/drivers
|
@ -264,15 +264,18 @@ func (p *PostgresDriver) ForeignKeyInfo(schema, tableName string) ([]bdb.Foreign
|
|||
|
||||
query := `
|
||||
select
|
||||
tc.constraint_name,
|
||||
kcu.table_name as source_table,
|
||||
kcu.column_name as source_column,
|
||||
ccu.table_name as dest_table,
|
||||
ccu.column_name as dest_column
|
||||
from information_schema.table_constraints as tc
|
||||
inner join information_schema.key_column_usage as kcu ON tc.constraint_name = kcu.constraint_name and tc.constraint_schema = kcu.constraint_schema
|
||||
inner join information_schema.constraint_column_usage as ccu ON tc.constraint_name = ccu.constraint_name and tc.constraint_schema = ccu.constraint_schema
|
||||
where tc.table_name = $1 and tc.constraint_type = 'FOREIGN KEY' and tc.table_schema = $2;`
|
||||
pgcon.conname,
|
||||
pgc.relname as source_table,
|
||||
pgasrc.attname as source_column,
|
||||
dstlookupname.relname as dest_table,
|
||||
pgadst.attname as dest_column
|
||||
from pg_namespace pgn
|
||||
inner join pg_class pgc on pgn.oid = pgc.relnamespace and pgc.relkind = 'r'
|
||||
inner join pg_constraint pgcon on pgn.oid = pgcon.connamespace and pgc.oid = pgcon.conrelid
|
||||
inner join pg_class dstlookupname on pgcon.confrelid = dstlookupname.oid
|
||||
inner join pg_attribute pgasrc on pgc.oid = pgasrc.attrelid and pgasrc.attnum = ANY(pgcon.conkey)
|
||||
inner join pg_attribute pgadst on pgcon.confrelid = pgadst.attrelid and pgadst.attnum = ANY(pgcon.confkey)
|
||||
where pgn.nspname = $2 and pgc.relname = $1 and pgcon.contype = 'f'`
|
||||
|
||||
var rows *sql.Rows
|
||||
var err error
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue