diff --git a/app/models/concerns/good_job/advisory_lockable.rb b/app/models/concerns/good_job/advisory_lockable.rb index e40ef0c8..7c714c93 100644 --- a/app/models/concerns/good_job/advisory_lockable.rb +++ b/app/models/concerns/good_job/advisory_lockable.rb @@ -41,8 +41,16 @@ module AdvisoryLockable scope :advisory_lock, (lambda do |column: _advisory_lockable_column, function: advisory_lockable_function, select_limit: nil| original_query = self + primary_key_for_select = primary_key.to_sym + column_for_select = column.to_sym + cte_table = Arel::Table.new(:rows) - cte_query = original_query.select(primary_key, column).except(:limit) + cte_query = original_query.except(:limit) + cte_query = if primary_key_for_select == column_for_select + cte_query.select(primary_key_for_select) + else + cte_query.select(primary_key_for_select, column_for_select) + end cte_query = cte_query.limit(select_limit) if select_limit cte_type = supports_cte_materialization_specifiers? ? :MATERIALIZED : :"" composed_cte = Arel::Nodes::As.new(cte_table, Arel::Nodes::UnaryOperation.new(cte_type, cte_query.arel)) diff --git a/spec/app/models/concerns/good_job/advisory_lockable_spec.rb b/spec/app/models/concerns/good_job/advisory_lockable_spec.rb index 15d019e8..c41565bc 100644 --- a/spec/app/models/concerns/good_job/advisory_lockable_spec.rb +++ b/spec/app/models/concerns/good_job/advisory_lockable_spec.rb @@ -51,7 +51,7 @@ FROM "good_jobs" WHERE "good_jobs"."id" IN ( WITH "rows" AS #{'MATERIALIZED' if model_class.supports_cte_materialization_specifiers?} ( - SELECT "good_jobs"."id", "good_jobs"."id" + SELECT "good_jobs"."id" FROM "good_jobs" WHERE "good_jobs"."priority" = 99 ORDER BY "good_jobs"."priority" DESC