Skip to content
This repository has been archived by the owner on Jun 4, 2020. It is now read-only.

Commit

Permalink
Merge pull request #24 from Roel/pg_pk_nonowner
Browse files Browse the repository at this point in the history
Fix pg_pk on tables not owned by user.
  • Loading branch information
vmora authored Dec 5, 2016
2 parents 3009694 + b707b13 commit 8aadb87
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions versioning_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,12 @@ def get_username():

def pg_pk( cur, schema_name, table_name ):
"""Fetch the primary key of the specified postgis table"""
cur.execute("SELECT c.column_name "
"FROM information_schema.table_constraints tc "
"JOIN information_schema.constraint_column_usage AS ccu "
"USING (constraint_schema, constraint_name) "
"JOIN information_schema.columns AS c "
"ON c.table_schema = tc.constraint_schema "
"AND tc.table_name = c.table_name "
"AND ccu.column_name = c.column_name "
"WHERE constraint_type = 'PRIMARY KEY' "
"AND tc.table_schema = '"+schema_name+"' "
"AND tc.table_name = '"+table_name+"'")
cur.execute("SELECT quote_ident(a.attname) as column_name "
"FROM pg_index i "
"JOIN pg_attribute a ON a.attrelid = i.indrelid "
"AND a.attnum = ANY(i.indkey) "
"WHERE i.indrelid = '\""+schema_name+'"."'+table_name+"\"'::regclass "
"AND i.indisprimary")
if not cur.hasrow():
raise RuntimeError("table "+schema_name+"."+table_name+
" does not have a primary key")
Expand Down

0 comments on commit 8aadb87

Please sign in to comment.