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

Having wrong before and after key values whenever I'm using select in query #148

Open
undefhero opened this issue Dec 23, 2021 · 5 comments
Assignees
Labels

Comments

@undefhero
Copy link

undefhero commented Dec 23, 2021

We've faced in a problem while we are using Paginator with Ecto.Query.select/3
There is a code sample:

query = from u in User, order_by: [asc: user.inserted_at, asc: user.id], select: %{user: u}
Backend.Repo.paginate(query, cursor_fields: [inserted_at: :asc, id: :asc])

Got a metadata:

%Paginator.Page.Metadata{
    after: "g3QAAAACZAACaWRkAANuaWxkAAtpbnNlcnRlZF9hdGQAA25pbA==",
    before: nil,
    limit: 2,
    total_count: 20,
    total_count_cap_exceeded: false
}

And after I applied it to fetch a next page I got it but before and after keys have the same values as a previous Metadata has.

%Paginator.Page.Metadata{
    after: "g3QAAAACZAACaWRkAANuaWxkAAtpbnNlcnRlZF9hdGQAA25pbA==",
    before: "g3QAAAACZAACaWRkAANuaWxkAAtpbnNlcnRlZF9hdGQAA25pbA==",
    limit: 2,
    total_count: 20,
    total_count_cap_exceeded: false
}

Any workaround here?

@undefhero
Copy link
Author

I also tried with :fetch_cursor_value_fun and had a similar result.

%Paginator.Page.Metadata{
    after: "g3QAAAABZAALaW5zZXJ0ZWRfYXR0AAAACWQACl9fc3RydWN0X19kABRFbGl4aXIuTmFpdmVEYXRlVGltZWQACGNhbGVuZGFyZAATRWxpeGlyLkNhbGVuZGFyLklTT2QAA2RheWEeZAAEaG91cmEOZAALbWljcm9zZWNvbmRoAmEAYQBkAAZtaW51dGVhBGQABW1vbnRoYQtkAAZzZWNvbmRhMGQABHllYXJiAAAH5Q==",
    before: "g3QAAAABZAALaW5zZXJ0ZWRfYXR0AAAACWQACl9fc3RydWN0X19kABRFbGl4aXIuTmFpdmVEYXRlVGltZWQACGNhbGVuZGFyZAATRWxpeGlyLkNhbGVuZGFyLklTT2QAA2RheWEeZAAEaG91cmEOZAALbWljcm9zZWNvbmRoAmEAYQBkAAZtaW51dGVhBGQABW1vbnRoYQtkAAZzZWNvbmRhMGQABHllYXJiAAAH5Q==",
    limit: 2,
    total_count: 20,
    total_count_cap_exceeded: false
  }

@sgerrand sgerrand self-assigned this Dec 23, 2021
@sgerrand
Copy link
Contributor

Thanks for raising this issue! Are you able to provide a small dataset to help us verify this bug?

@undefhero
Copy link
Author

undefhero commented Dec 27, 2021

@sgerrand thank you for your response. I was using the paginator database to confirm the issue

query = from(c in Payment, order_by: [asc: c.inserted_at, asc: c.id], select: %{c: c})

%{metadata: %{after: cursor}} = Repo.paginate(query, cursor_fields: [inserted_at: :asc, id: :asc], limit: 3)
%{metadata: %{before: ^cursor, after: ^cursor}} = 
  Repo.paginate(query, cursor_fields: [inserted_at: :asc, id: :asc], after: cursor, limit: 3)

https://github.com/undefhero/paginator/blob/8f1841f6330c5a9aaa59d6d9a240583a910d4dd7/test/paginator_test.exs#L667

@nicolasblanco
Copy link

Hello!

I can confirm this bugs happens in my program when I'm using a select in my query.

If I can help debug this issue, tell me.

@nicolasblanco
Copy link

I have found the solution.

You just need to be sure that in the select of the query you correctly have at least all the fields that you use in the cursor_fields option.

It worked for me 🙌🏻 .

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

No branches or pull requests

3 participants