Skip to content

Commit aca12a6

Browse files
committed
Stop returning primary key constraints in get_indexes
This is done by using the Postgres dialect's get_indexes implementation if on v19.2+ of CockroachDB.
1 parent 52f8698 commit aca12a6

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

cockroachdb/sqlalchemy/dialect.py

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -255,35 +255,27 @@ def get_columns(self, conn, table_name, schema=None, **kw):
255255
return res
256256

257257
def get_indexes(self, conn, table_name, schema=None, **kw):
258+
if self._is_v192plus:
259+
return super().get_indexes(conn, table_name, schema, **kw)
260+
258261
# The Cockroach database creates a UNIQUE INDEX implicitly whenever the
259262
# UNIQUE CONSTRAINT construct is used. Currently we are just ignoring all unique indexes,
260263
# but we might need to return them and add an additional key `duplicates_constraint` if
261264
# it is detected as mirroring a constraint.
262265
# https://www.cockroachlabs.com/docs/stable/unique.html
263266
# https://github.com/sqlalchemy/sqlalchemy/blob/55f930ef3d4e60bed02a2dad16e331fe42cfd12b/lib/sqlalchemy/dialects/postgresql/base.py#L723
264-
if not self._is_v2plus:
265-
q = '''
266-
SELECT
267-
"Name" as index_name,
268-
"Column" as column_name,
269-
"Unique" as unique,
270-
"Implicit" as implicit
271-
FROM
272-
[SHOW INDEXES FROM "%(schema)s"."%(name)s"]
273-
'''
274-
else:
275-
q = '''
276-
SELECT
277-
index_name,
278-
column_name,
279-
(not non_unique::bool) as unique,
280-
implicit::bool as implicit
281-
FROM
282-
information_schema.statistics
283-
WHERE
284-
table_schema = %(schema)s
285-
AND table_name = %(name)s
286-
'''
267+
q = '''
268+
SELECT
269+
index_name,
270+
column_name,
271+
(not non_unique::bool) as unique,
272+
implicit::bool as implicit
273+
FROM
274+
information_schema.statistics
275+
WHERE
276+
table_schema = %(schema)s
277+
AND table_name = %(name)s
278+
'''
287279
rows = conn.execute(q, schema=(schema or self.default_schema_name), name=table_name)
288280
indexes = collections.defaultdict(list)
289281
for row in rows:

0 commit comments

Comments
 (0)