mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
why not use JOIN statement for joining tables? (vol. 2)
This commit is contained in:
@@ -820,18 +820,13 @@ module ActiveRecord
|
||||
# given table's primary key.
|
||||
result = exec_query(<<-end_sql, 'SCHEMA').rows.first
|
||||
SELECT attr.attname, seq.relname
|
||||
FROM pg_class seq,
|
||||
pg_attribute attr,
|
||||
pg_depend dep,
|
||||
pg_constraint cons
|
||||
WHERE seq.oid = dep.objid
|
||||
AND seq.relkind = 'S'
|
||||
AND attr.attrelid = dep.refobjid
|
||||
AND attr.attnum = dep.refobjsubid
|
||||
AND attr.attrelid = cons.conrelid
|
||||
AND attr.attnum = cons.conkey[1]
|
||||
AND cons.contype = 'p'
|
||||
AND dep.refobjid = '#{quote_table_name(table)}'::regclass
|
||||
FROM pg_class seq
|
||||
INNER JOIN pg_depend dep on seq.oid = dep.objid
|
||||
INNER JOIN pg_attribute attr ON attr.attrelid = dep.refobjid AND attr.attnum = dep.refobjsubid
|
||||
INNER JOIN pg_constraint cons ON attr.attrelid = cons.conrelid AND attr.attnum = cons.conkey[1]
|
||||
WHERE seq.relkind = 'S'
|
||||
AND cons.contype = 'p'
|
||||
AND dep.refobjid = '#{quote_table_name(table)}'::regclass
|
||||
end_sql
|
||||
|
||||
# [primary_key, sequence]
|
||||
@@ -844,15 +839,11 @@ module ActiveRecord
|
||||
def primary_key(table)
|
||||
row = exec_query(<<-end_sql, 'SCHEMA', [[nil, table]]).rows.first
|
||||
SELECT DISTINCT(attr.attname)
|
||||
FROM pg_attribute attr,
|
||||
pg_depend dep,
|
||||
pg_constraint cons
|
||||
WHERE attr.attrelid = dep.refobjid
|
||||
AND attr.attnum = dep.refobjsubid
|
||||
AND attr.attrelid = cons.conrelid
|
||||
AND attr.attnum = cons.conkey[1]
|
||||
AND cons.contype = 'p'
|
||||
AND dep.refobjid = $1::regclass
|
||||
FROM pg_attribute attr
|
||||
INNER JOIN pg_depend dep ON attr.attrelid = dep.refobjid AND attr.attnum = dep.refobjsubid
|
||||
INNER JOIN pg_constraint cons ON attr.attrelid = cons.conrelid AND attr.attnum = cons.conkey[1]
|
||||
WHERE cons.contype = 'p'
|
||||
AND dep.refobjid = $1::regclass
|
||||
end_sql
|
||||
|
||||
row && row.first
|
||||
|
||||
Reference in New Issue
Block a user