summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGES3
-rw-r--r--lib/sqlalchemy/dialects/firebird/base.py7
-rw-r--r--lib/sqlalchemy/dialects/informix/base.py10
-rw-r--r--lib/sqlalchemy/dialects/mssql/base.py4
-rw-r--r--lib/sqlalchemy/dialects/mysql/base.py8
-rw-r--r--lib/sqlalchemy/dialects/oracle/base.py33
-rw-r--r--lib/sqlalchemy/dialects/postgresql/base.py19
-rw-r--r--lib/sqlalchemy/dialects/sqlite/base.py4
-rw-r--r--lib/sqlalchemy/engine/base.py19
-rw-r--r--lib/sqlalchemy/engine/reflection.py25
-rw-r--r--test/engine/test_reflection.py46
11 files changed, 83 insertions, 95 deletions
diff --git a/CHANGES b/CHANGES
index 8d3153bed..95ae66802 100644
--- a/CHANGES
+++ b/CHANGES
@@ -146,6 +146,9 @@ CHANGES
that have a distinctly named .key.
[ticket:2397]
+ - [feature] Inspector.get_primary_keys() is
+ deprecated; use Inspector.get_pk_constraint().
+ Courtesy Diana Clarke. [ticket:2422]
- sql
- [feature] The Inspector object can now be
diff --git a/lib/sqlalchemy/dialects/firebird/base.py b/lib/sqlalchemy/dialects/firebird/base.py
index 5ef30b36d..a0e48da2c 100644
--- a/lib/sqlalchemy/dialects/firebird/base.py
+++ b/lib/sqlalchemy/dialects/firebird/base.py
@@ -474,7 +474,7 @@ class FBDialect(default.DefaultDialect):
return None
@reflection.cache
- def get_primary_keys(self, connection, table_name, schema=None, **kw):
+ def get_pk_constraint(self, connection, table_name, schema=None, **kw):
# Query to extract the PK/FK constrained fields of the given table
keyqry = """
SELECT se.rdb$field_name AS fname
@@ -486,7 +486,7 @@ class FBDialect(default.DefaultDialect):
# get primary key fields
c = connection.execute(keyqry, ["PRIMARY KEY", tablename])
pkfields = [self.normalize_name(r['fname']) for r in c.fetchall()]
- return pkfields
+ return {'constrained_columns':pkfields, 'name':None}
@reflection.cache
def get_column_sequence(self, connection,
@@ -541,7 +541,8 @@ class FBDialect(default.DefaultDialect):
ORDER BY r.rdb$field_position
"""
# get the PK, used to determine the eventual associated sequence
- pkey_cols = self.get_primary_keys(connection, table_name)
+ pk_constraint = self.get_pk_constraint(connection, table_name)
+ pkey_cols = pk_constraint['constrained_columns']
tablename = self.denormalize_name(table_name)
# get all of the fields for this table
diff --git a/lib/sqlalchemy/dialects/informix/base.py b/lib/sqlalchemy/dialects/informix/base.py
index 7880d1c14..044fb525d 100644
--- a/lib/sqlalchemy/dialects/informix/base.py
+++ b/lib/sqlalchemy/dialects/informix/base.py
@@ -411,7 +411,8 @@ class InformixDialect(default.DefaultDialect):
and t3.tabid = t2.tabid and t3.colno = t1.colno
order by t1.colno""", table_name, schema)
- primary_cols = self.get_primary_keys(connection, table_name, schema, **kw)
+ pk_constraint = self.get_pk_constraint(connection, table_name, schema, **kw)
+ primary_cols = pk_constraint['constrained_columns']
columns = []
rows = c.fetchall()
@@ -514,7 +515,7 @@ class InformixDialect(default.DefaultDialect):
return fkeys.values()
@reflection.cache
- def get_primary_keys(self, connection, table_name, schema=None, **kw):
+ def get_pk_constraint(self, connection, table_name, schema=None, **kw):
schema = schema or self.default_schema_name
# Select the column positions from sysindexes for sysconstraints
@@ -533,7 +534,7 @@ class InformixDialect(default.DefaultDialect):
colpositions |= colpos
if not len(colpositions):
- return []
+ return {'constrained_columns':[], 'name':None}
# Select the column names using the columnpositions
# TODO: Maybe cache a bit of those col infos (eg select all colnames for one table)
@@ -546,7 +547,8 @@ class InformixDialect(default.DefaultDialect):
table_name, *colpositions
).fetchall()
- return reduce(lambda x,y: list(x)+list(y), c, [])
+ cols = reduce(lambda x,y: list(x)+list(y), c, [])
+ return {'constrained_columns':cols, 'name':None}
@reflection.cache
def get_indexes(self, connection, table_name, schema, **kw):
diff --git a/lib/sqlalchemy/dialects/mssql/base.py b/lib/sqlalchemy/dialects/mssql/base.py
index e5eb44744..4d0af7cbe 100644
--- a/lib/sqlalchemy/dialects/mssql/base.py
+++ b/lib/sqlalchemy/dialects/mssql/base.py
@@ -1391,7 +1391,7 @@ class MSDialect(default.DefaultDialect):
return cols
@reflection.cache
- def get_primary_keys(self, connection, tablename, schema=None, **kw):
+ def get_pk_constraint(self, connection, tablename, schema=None, **kw):
current_schema = schema or self.default_schema_name
pkeys = []
# information_schema.referential_constraints
@@ -1415,7 +1415,7 @@ class MSDialect(default.DefaultDialect):
for row in c:
if 'PRIMARY' in row[TC.c.constraint_type.name]:
pkeys.append(row[0])
- return pkeys
+ return {'constrained_columns':pkeys, 'name':None}
@reflection.cache
def get_foreign_keys(self, connection, tablename, schema=None, **kw):
diff --git a/lib/sqlalchemy/dialects/mysql/base.py b/lib/sqlalchemy/dialects/mysql/base.py
index d28e934a3..a61d59e9b 100644
--- a/lib/sqlalchemy/dialects/mysql/base.py
+++ b/lib/sqlalchemy/dialects/mysql/base.py
@@ -2045,14 +2045,14 @@ class MySQLDialect(default.DefaultDialect):
return parsed_state.columns
@reflection.cache
- def get_primary_keys(self, connection, table_name, schema=None, **kw):
+ def get_pk_constraint(self, connection, table_name, schema=None, **kw):
parsed_state = self._parsed_state_or_create(connection, table_name, schema, **kw)
for key in parsed_state.keys:
if key['type'] == 'PRIMARY':
# There can be only one.
- ##raise Exception, str(key)
- return [s[0] for s in key['columns']]
- return []
+ cols = [s[0] for s in key['columns']]
+ return {'constrained_columns':cols, 'name':None}
+ return {'constrained_columns':[], 'name':None}
@reflection.cache
def get_foreign_keys(self, connection, table_name, schema=None, **kw):
diff --git a/lib/sqlalchemy/dialects/oracle/base.py b/lib/sqlalchemy/dialects/oracle/base.py
index dd761ae82..500b1995f 100644
--- a/lib/sqlalchemy/dialects/oracle/base.py
+++ b/lib/sqlalchemy/dialects/oracle/base.py
@@ -941,10 +941,10 @@ class OracleDialect(default.DefaultDialect):
schema=self.denormalize_name(schema))
indexes = []
last_index_name = None
- pkeys = self.get_primary_keys(connection, table_name, schema,
- resolve_synonyms=resolve_synonyms,
- dblink=dblink,
- info_cache=kw.get('info_cache'))
+ pk_constraint = self.get_pk_constraint(
+ connection, table_name, schema, resolve_synonyms=resolve_synonyms,
+ dblink=dblink, info_cache=kw.get('info_cache'))
+ pkeys = pk_constraint['constrained_columns']
uniqueness = dict(NONUNIQUE=False, UNIQUE=True)
oracle_sys_col = re.compile(r'SYS_NC\d+\$', re.IGNORECASE)
@@ -1006,20 +1006,8 @@ class OracleDialect(default.DefaultDialect):
constraint_data = rp.fetchall()
return constraint_data
- def get_primary_keys(self, connection, table_name, schema=None, **kw):
- """
-
- kw arguments can be:
-
- oracle_resolve_synonyms
-
- dblink
-
- """
- return self._get_primary_keys(connection, table_name, schema, **kw)[0]
-
@reflection.cache
- def _get_primary_keys(self, connection, table_name, schema=None, **kw):
+ def get_pk_constraint(self, connection, table_name, schema=None, **kw):
resolve_synonyms = kw.get('oracle_resolve_synonyms', False)
dblink = kw.get('dblink', '')
info_cache = kw.get('info_cache')
@@ -1035,22 +1023,13 @@ class OracleDialect(default.DefaultDialect):
info_cache=kw.get('info_cache'))
for row in constraint_data:
- #print "ROW:" , row
(cons_name, cons_type, local_column, remote_table, remote_column, remote_owner) = \
row[0:2] + tuple([self.normalize_name(x) for x in row[2:6]])
if cons_type == 'P':
if constraint_name is None:
constraint_name = self.normalize_name(cons_name)
pkeys.append(local_column)
- return pkeys, constraint_name
-
- def get_pk_constraint(self, connection, table_name, schema=None, **kw):
- cols, name = self._get_primary_keys(connection, table_name, schema=schema, **kw)
-
- return {
- 'constrained_columns':cols,
- 'name':name
- }
+ return {'constrained_columns':pkeys, 'name':constraint_name}
@reflection.cache
def get_foreign_keys(self, connection, table_name, schema=None, **kw):
diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py
index cc1aa7600..d47b9e757 100644
--- a/lib/sqlalchemy/dialects/postgresql/base.py
+++ b/lib/sqlalchemy/dialects/postgresql/base.py
@@ -1408,7 +1408,7 @@ class PGDialect(default.DefaultDialect):
return columns
@reflection.cache
- def get_primary_keys(self, connection, table_name, schema=None, **kw):
+ def get_pk_constraint(self, connection, table_name, schema=None, **kw):
table_oid = self.get_table_oid(connection, table_name, schema,
info_cache=kw.get('info_cache'))
@@ -1427,16 +1427,7 @@ class PGDialect(default.DefaultDialect):
"""
t = sql.text(PK_SQL, typemap={'attname':sqltypes.Unicode})
c = connection.execute(t, table_oid=table_oid)
- primary_keys = [r[0] for r in c.fetchall()]
- return primary_keys
-
- @reflection.cache
- def get_pk_constraint(self, connection, table_name, schema=None, **kw):
- cols = self.get_primary_keys(connection, table_name,
- schema=schema, **kw)
-
- table_oid = self.get_table_oid(connection, table_name, schema,
- info_cache=kw.get('info_cache'))
+ cols = [r[0] for r in c.fetchall()]
PK_CONS_SQL = """
SELECT conname
@@ -1447,10 +1438,8 @@ class PGDialect(default.DefaultDialect):
t = sql.text(PK_CONS_SQL, typemap={'conname':sqltypes.Unicode})
c = connection.execute(t, table_oid=table_oid)
name = c.scalar()
- return {
- 'constrained_columns':cols,
- 'name':name
- }
+
+ return {'constrained_columns':cols, 'name':name}
@reflection.cache
def get_foreign_keys(self, connection, table_name, schema=None, **kw):
diff --git a/lib/sqlalchemy/dialects/sqlite/base.py b/lib/sqlalchemy/dialects/sqlite/base.py
index 9582db8f8..754bf7966 100644
--- a/lib/sqlalchemy/dialects/sqlite/base.py
+++ b/lib/sqlalchemy/dialects/sqlite/base.py
@@ -713,13 +713,13 @@ class SQLiteDialect(default.DefaultDialect):
return columns
@reflection.cache
- def get_primary_keys(self, connection, table_name, schema=None, **kw):
+ def get_pk_constraint(self, connection, table_name, schema=None, **kw):
cols = self.get_columns(connection, table_name, schema, **kw)
pkeys = []
for col in cols:
if col['primary_key']:
pkeys.append(col['name'])
- return pkeys
+ return {'constrained_columns':pkeys, 'name':None}
@reflection.cache
def get_foreign_keys(self, connection, table_name, schema=None, **kw):
diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py
index 93d2b19f1..a2695e337 100644
--- a/lib/sqlalchemy/engine/base.py
+++ b/lib/sqlalchemy/engine/base.py
@@ -261,20 +261,23 @@ class Dialect(object):
def get_primary_keys(self, connection, table_name, schema=None, **kw):
"""Return information about primary keys in `table_name`.
-
- Given a :class:`.Connection`, a string
- `table_name`, and an optional string `schema`, return primary
- key information as a list of column names.
-
+
+
+ Deprecated. This method is only called by the default
+ implementation of :meth:`get_pk_constraint()`. Dialects should
+ instead implement this method directly.
+
"""
+
raise NotImplementedError()
- def get_pk_constraint(self, table_name, schema=None, **kw):
+ def get_pk_constraint(self, connection, table_name, schema=None, **kw):
"""Return information about the primary key constraint on
table_name`.
- Given a string `table_name`, and an optional string `schema`, return
- primary key information as a dictionary with these keys:
+ Given a :class:`.Connection`, a string
+ `table_name`, and an optional string `schema`, return primary
+ key information as a dictionary with these keys:
constrained_columns
a list of column names that make up the primary key
diff --git a/lib/sqlalchemy/engine/reflection.py b/lib/sqlalchemy/engine/reflection.py
index 4ad3595c3..13a7e1b88 100644
--- a/lib/sqlalchemy/engine/reflection.py
+++ b/lib/sqlalchemy/engine/reflection.py
@@ -26,10 +26,11 @@ methods such as get_table_names, get_columns, etc.
import sqlalchemy
from sqlalchemy import exc, sql
+from sqlalchemy import schema as sa_schema
from sqlalchemy import util
-from sqlalchemy.util import topological
from sqlalchemy.types import TypeEngine
-from sqlalchemy import schema as sa_schema
+from sqlalchemy.util import deprecated
+from sqlalchemy.util import topological
from sqlalchemy import inspection
from sqlalchemy.engine.base import Connectable
@@ -233,6 +234,8 @@ class Inspector(object):
col_def['type'] = coltype()
return col_defs
+ @deprecated('0.7', 'Call to deprecated method get_primary_keys.'
+ ' Use get_pk_constraint instead.')
def get_primary_keys(self, table_name, schema=None, **kw):
"""Return information about primary keys in `table_name`.
@@ -240,11 +243,9 @@ class Inspector(object):
primary key information as a list of column names.
"""
- pkeys = self.dialect.get_primary_keys(self.bind, table_name, schema,
- info_cache=self.info_cache,
- **kw)
-
- return pkeys
+ return self.dialect.get_pk_constraint(self.bind, table_name, schema,
+ info_cache=self.info_cache,
+ **kw)['constrained_columns']
def get_pk_constraint(self, table_name, schema=None, **kw):
"""Return information about primary key constraint on `table_name`.
@@ -259,12 +260,10 @@ class Inspector(object):
optional name of the primary key constraint.
"""
- pkeys = self.dialect.get_pk_constraint(self.bind, table_name, schema,
+ return self.dialect.get_pk_constraint(self.bind, table_name, schema,
info_cache=self.info_cache,
**kw)
- return pkeys
-
def get_foreign_keys(self, table_name, schema=None, **kw):
"""Return information about foreign_keys in `table_name`.
@@ -293,10 +292,9 @@ class Inspector(object):
"""
- fk_defs = self.dialect.get_foreign_keys(self.bind, table_name, schema,
+ return self.dialect.get_foreign_keys(self.bind, table_name, schema,
info_cache=self.info_cache,
**kw)
- return fk_defs
def get_indexes(self, table_name, schema=None, **kw):
"""Return information about indexes in `table_name`.
@@ -317,10 +315,9 @@ class Inspector(object):
other options passed to the dialect's get_indexes() method.
"""
- indexes = self.dialect.get_indexes(self.bind, table_name,
+ return self.dialect.get_indexes(self.bind, table_name,
schema,
info_cache=self.info_cache, **kw)
- return indexes
def reflecttable(self, table, include_columns, exclude_columns=()):
"""Given a Table object, load its internal constructs based on introspection.
diff --git a/test/engine/test_reflection.py b/test/engine/test_reflection.py
index 371a4c1b4..6bfc3246a 100644
--- a/test/engine/test_reflection.py
+++ b/test/engine/test_reflection.py
@@ -1,13 +1,14 @@
-from test.lib.testing import eq_, assert_raises, assert_raises_message
-import StringIO, unicodedata
+import unicodedata
+import sqlalchemy as sa
+from sqlalchemy import exc as sa_exc
from sqlalchemy import types as sql_types
from sqlalchemy import schema, events, event, inspect
from sqlalchemy import MetaData, Integer, String
-from test.lib.schema import Table, Column
-import sqlalchemy as sa
+from sqlalchemy.engine.reflection import Inspector
from test.lib import ComparesTables, \
- testing, engines, AssertsCompiledSQL
-from test.lib import fixtures
+ testing, engines, AssertsCompiledSQL, fixtures
+from test.lib.schema import Table, Column
+from test.lib.testing import eq_, assert_raises, assert_raises_message
metadata, users = None, None
@@ -1483,17 +1484,17 @@ class ComponentReflectionTest(fixtures.TestBase):
self._test_get_columns(schema='test_schema', table_type='view')
@testing.provide_metadata
- def _test_get_primary_keys(self, schema=None):
+ def _test_get_pk_constraint(self, schema=None):
meta = self.metadata
- users, addresses, dingalings = createTables(meta, schema)
+ users, addresses, _ = createTables(meta, schema)
meta.create_all()
insp = inspect(meta.bind)
- users_pkeys = insp.get_primary_keys(users.name,
- schema=schema)
+
+ users_cons = insp.get_pk_constraint(users.name, schema=schema)
+ users_pkeys = users_cons['constrained_columns']
eq_(users_pkeys, ['user_id'])
- addr_cons = insp.get_pk_constraint(addresses.name,
- schema=schema)
+ addr_cons = insp.get_pk_constraint(addresses.name, schema=schema)
addr_pkeys = addr_cons['constrained_columns']
eq_(addr_pkeys, ['address_id'])
@@ -1502,12 +1503,25 @@ class ComponentReflectionTest(fixtures.TestBase):
eq_(addr_cons['name'], 'email_ad_pk')
go()
- def test_get_primary_keys(self):
- self._test_get_primary_keys()
+ def test_get_pk_constraint(self):
+ self._test_get_pk_constraint()
@testing.fails_on('sqlite', 'no schemas')
- def test_get_primary_keys_with_schema(self):
- self._test_get_primary_keys(schema='test_schema')
+ def test_get_pk_constraint_with_schema(self):
+ self._test_get_pk_constraint(schema='test_schema')
+
+ @testing.provide_metadata
+ def test_deprecated_get_primary_keys(self):
+ meta = self.metadata
+ users, _, _ = createTables(meta, schema=None)
+ meta.create_all()
+ insp = Inspector(meta.bind)
+ assert_raises_message(
+ sa_exc.SADeprecationWarning,
+ "Call to deprecated method get_primary_keys."
+ " Use get_pk_constraint instead.",
+ insp.get_primary_keys, users.name
+ )
@testing.provide_metadata
def _test_get_foreign_keys(self, schema=None):