Skip to content

Composite Primary keys not working #511

@davidwparker

Description

@davidwparker

Hello.

I have the following model:

class Model < ApplicationRecord
  self.primary_key = [:sub_id, :id]
....

When I call find_each on it, I get results like this:

SELECT "models".* FROM "models" WHERE "model"."sub_id" = $1 AND (("models"."sub_id" > 'xxxxx-yyy-...') OR ("models"."sub_id" = 'xxxxx-yyy-...' AND "models"."id" > 'aaaaa-bb....')) ORDER BY "models"."sub_id" ASC, "models"."id" ASC LIMIT $4

But when calling with this library, I'd expect the same, but am getting different:

# frozen_string_literal: true

class MyJob < ApplicationIterationJob
  BATCH_SIZE = 2

  def build_enumerator(sub_id, cursor:)
    enumerator_builder.active_record_on_batches(
      Model.where(sub_id: sub_id),
      cursor: cursor,
      batch_size: BATCH_SIZE
    )
  end

  def each_iteration(models, _sub_id)
...
  end
end

results in

SELECT "models".* FROM "models" WHERE "models"."sub_id" = $1 AND (models.sub_id > 'xxxxx-yyy-....' OR (models.sub_id = 'xxxxx-yyy-....' AND (models.id > 'xxxxx-yyy-....','aaaaa-bb.....'))) ORDER BY models.sub_id,models.id LIMIT $4 

ActiveRecord::StatementInvalid: PG::DatatypeMismatch: ERROR:  argument of AND must be type boolean, not type record
LINE 1: ...y_id = 'xxxxx-yyy....' AND (company_f...

And it's due to the fact that (models.id > 'xxxxx-yyy-....','aaaaa-bb.....') should just be using aaaaa-bb... instead of the whole thing.

I've checked the docs, and previous tickets, but not seeing this come up.

Relevant PR #365

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions