Fragment does not allow strings to be interpolated

I’ve created new column use fragment in select, then to use that new column, I need to use fragment("column_name"). This work for my code as column name is literal, but when I use other library such as duffelhq/paginator: Cursor-based pagination for Elixir Ecto (github.com) with this column name it will cause error “fragment does not allow strings to be interpolated”.

query
|> Ecto.Query.select([table], %{row: fragment("(case when .... end) as column_name")})
|> repo.paginate(cursor_fields: ["column_name"], limit: limit, after: cursor_after)

Paginator in background just generate a where clause then need some way to pass fragment(^column_name) in where clause. In this case, fragment is safe as first value was pass as literal to library. fragment("?", ^column_name) won’t work too as it shows error “could not determine data type of parameter”.

Here is a PR Add expression based cursor by nikolay-slavov · Pull Request #60 · duffelhq/paginator · GitHub which may solve your issue

1 Like