@@ -184,14 +184,25 @@ final class RedshiftPluginDriver: PluginDatabaseDriver, @unchecked Sendable {
184184 c.column_default,
185185 c.collation_name,
186186 pgd.description,
187- c.udt_name
187+ c.udt_name,
188+ CASE WHEN pk.column_name IS NOT NULL THEN 'YES' ELSE 'NO' END AS is_pk
188189 FROM information_schema.columns c
189190 LEFT JOIN pg_catalog.pg_class cls
190191 ON cls.relname = c.table_name
191192 AND cls.relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = c.table_schema)
192193 LEFT JOIN pg_catalog.pg_description pgd
193194 ON pgd.objoid = cls.oid
194195 AND pgd.objsubid = c.ordinal_position
196+ LEFT JOIN (
197+ SELECT DISTINCT kcu.column_name
198+ FROM information_schema.table_constraints tc
199+ JOIN information_schema.key_column_usage kcu
200+ ON tc.constraint_name = kcu.constraint_name
201+ AND tc.table_schema = kcu.table_schema
202+ WHERE tc.constraint_type = 'PRIMARY KEY'
203+ AND tc.table_schema = ' \( escapedSchema) '
204+ AND tc.table_name = ' \( safeTable) '
205+ ) pk ON c.column_name = pk.column_name
195206 WHERE c.table_schema = ' \( escapedSchema) ' AND c.table_name = ' \( safeTable) '
196207 ORDER BY c.ordinal_position
197208 """
@@ -214,6 +225,7 @@ final class RedshiftPluginDriver: PluginDatabaseDriver, @unchecked Sendable {
214225 let defaultValue = row [ 3 ]
215226 let collation = row. count > 4 ? row [ 4 ] : nil
216227 let comment = row. count > 5 ? row [ 5 ] : nil
228+ let isPk = row. count > 7 && row [ 7 ] == " YES "
217229
218230 let charset : String ? = {
219231 guard let coll = collation else { return nil }
@@ -227,7 +239,7 @@ final class RedshiftPluginDriver: PluginDatabaseDriver, @unchecked Sendable {
227239 name: name,
228240 dataType: dataType,
229241 isNullable: isNullable,
230- isPrimaryKey: false ,
242+ isPrimaryKey: isPk ,
231243 defaultValue: defaultValue,
232244 charset: charset,
233245 collation: collation,
@@ -246,14 +258,24 @@ final class RedshiftPluginDriver: PluginDatabaseDriver, @unchecked Sendable {
246258 c.column_default,
247259 c.collation_name,
248260 pgd.description,
249- c.udt_name
261+ c.udt_name,
262+ CASE WHEN pk.column_name IS NOT NULL THEN 'YES' ELSE 'NO' END AS is_pk
250263 FROM information_schema.columns c
251264 LEFT JOIN pg_catalog.pg_class cls
252265 ON cls.relname = c.table_name
253266 AND cls.relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = c.table_schema)
254267 LEFT JOIN pg_catalog.pg_description pgd
255268 ON pgd.objoid = cls.oid
256269 AND pgd.objsubid = c.ordinal_position
270+ LEFT JOIN (
271+ SELECT DISTINCT kcu.table_name, kcu.column_name
272+ FROM information_schema.table_constraints tc
273+ JOIN information_schema.key_column_usage kcu
274+ ON tc.constraint_name = kcu.constraint_name
275+ AND tc.table_schema = kcu.table_schema
276+ WHERE tc.constraint_type = 'PRIMARY KEY'
277+ AND tc.table_schema = ' \( escapedSchema) '
278+ ) pk ON c.table_name = pk.table_name AND c.column_name = pk.column_name
257279 WHERE c.table_schema = ' \( escapedSchema) '
258280 ORDER BY c.table_name, c.ordinal_position
259281 """
@@ -278,6 +300,7 @@ final class RedshiftPluginDriver: PluginDatabaseDriver, @unchecked Sendable {
278300 let defaultValue = row [ 4 ]
279301 let collation = row. count > 5 ? row [ 5 ] : nil
280302 let comment = row. count > 6 ? row [ 6 ] : nil
303+ let isPk = row. count > 8 && row [ 8 ] == " YES "
281304
282305 let charset : String ? = {
283306 guard let coll = collation else { return nil }
@@ -291,7 +314,7 @@ final class RedshiftPluginDriver: PluginDatabaseDriver, @unchecked Sendable {
291314 name: name,
292315 dataType: dataType,
293316 isNullable: isNullable,
294- isPrimaryKey: false ,
317+ isPrimaryKey: isPk ,
295318 defaultValue: defaultValue,
296319 charset: charset,
297320 collation: collation,
0 commit comments