Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Composite Primary keys not working #511

Open
davidwparker opened this issue Oct 8, 2024 · 0 comments
Open

Composite Primary keys not working #511

davidwparker opened this issue Oct 8, 2024 · 0 comments

Comments

@davidwparker
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant