@@ -57,6 +57,7 @@ def primary_key(table_name)
5757
5858 # OVERRIDE: Added `unique_rowid` to the last line of the second query.
5959 # This is a CockroachDB-specific function used for primary keys.
60+ # Also make sure we don't consider `NOT VISIBLE` columns.
6061 #
6162 # Returns a table's primary key and belonging sequence.
6263 def pk_and_sequence_for ( table ) # :nodoc:
@@ -68,14 +69,21 @@ def pk_and_sequence_for(table) # :nodoc:
6869 pg_attribute attr,
6970 pg_depend dep,
7071 pg_constraint cons,
71- pg_namespace nsp
72+ pg_namespace nsp,
73+ -- TODO: use the pg_catalog.pg_attribute(attishidden) column when
74+ -- it is added instead of joining on crdb_internal.
75+ -- See https://github.com/cockroachdb/cockroach/pull/126397
76+ crdb_internal.table_columns tc
7277 WHERE seq.oid = dep.objid
7378 AND seq.relkind = 'S'
7479 AND attr.attrelid = dep.refobjid
7580 AND attr.attnum = dep.refobjsubid
7681 AND attr.attrelid = cons.conrelid
7782 AND attr.attnum = cons.conkey[1]
7883 AND seq.relnamespace = nsp.oid
84+ AND attr.attrelid = tc.descriptor_id
85+ AND attr.attname = tc.column_name
86+ AND tc.hidden = false
7987 AND cons.contype = 'p'
8088 AND dep.classid = 'pg_class'::regclass
8189 AND dep.refobjid = #{ quote ( quote_table_name ( table ) ) } ::regclass
@@ -96,7 +104,12 @@ def pk_and_sequence_for(table) # :nodoc:
96104 JOIN pg_attrdef def ON (adrelid = attrelid AND adnum = attnum)
97105 JOIN pg_constraint cons ON (conrelid = adrelid AND adnum = conkey[1])
98106 JOIN pg_namespace nsp ON (t.relnamespace = nsp.oid)
107+ -- TODO: use the pg_catalog.pg_attribute(attishidden) column when
108+ -- it is added instead of joining on crdb_internal.
109+ -- See https://github.com/cockroachdb/cockroach/pull/126397
110+ JOIN crdb_internal.table_columns tc ON (attr.attrelid = tc.descriptor_id AND attr.attname = tc.column_name)
99111 WHERE t.oid = #{ quote ( quote_table_name ( table ) ) } ::regclass
112+ AND tc.hidden = false
100113 AND cons.contype = 'p'
101114 AND pg_get_expr(def.adbin, def.adrelid) ~* 'nextval|uuid_generate|gen_random_uuid|unique_rowid'
102115 SQL
0 commit comments