Skip to content

Commit 7cdc3fe

Browse files
authored
Picking only one table for the primary key
1 parent b82bafb commit 7cdc3fe

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

tap_mssql/__init__.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -230,18 +230,31 @@ def discover_catalog(mssql_conn, config):
230230
table_info[db][table] = {"row_count": None, "is_view": table_type == "VIEW"}
231231
LOGGER.info("Tables fetched, fetching columns")
232232
cur.execute(
233-
"""with constraint_columns as (
233+
""" with table_constraints as (
234+
select tc.TABLE_SCHEMA,
235+
tc.TABLE_NAME,
236+
tc.CONSTRAINT_NAME,
237+
tc.CONSTRAINT_TYPE,
238+
row_number() over (partition by tc.TABLE_SCHEMA, tc.TABLE_NAME
239+
order by tc.constraint_TYPE) as row_number_rank
240+
241+
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
242+
where tc.CONSTRAINT_TYPE in ('PRIMARY KEY', 'UNIQUE')
243+
)
244+
,constraint_columns as (
234245
select c.TABLE_SCHEMA
235246
, c.TABLE_NAME
236247
, c.COLUMN_NAME
248+
, c.CONSTRAINT_NAME
237249
238250
from INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE c
239251
240-
join INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
252+
join table_constraints tc
241253
on tc.TABLE_SCHEMA = c.TABLE_SCHEMA
242254
and tc.TABLE_NAME = c.TABLE_NAME
243255
and tc.CONSTRAINT_NAME = c.CONSTRAINT_NAME
244-
and tc.CONSTRAINT_TYPE in ('PRIMARY KEY', 'UNIQUE'))
256+
and tc.row_number_rank = 1
257+
)
245258
SELECT c.TABLE_SCHEMA,
246259
c.TABLE_NAME,
247260
c.COLUMN_NAME,

0 commit comments

Comments
 (0)