8000 Docs: Don't reference deprecated `literal` by ftes · Pull Request #4627 · elixir-ecto/ecto · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Docs: Don't reference deprecated literal #4627

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

Merged
merged 2 commits into from
Jun 23, 2025

Conversation

ftes
Copy link
Contributor
@ftes ftes commented Jun 23, 2025

This is probably not the best fix for the docs.

Additional ideas (not done):

  • use different wording instead of literal
  • reference the identifier and constant function docs
  • explain the difference between identifier and constant

@greg-rychlewski
Copy link
Member

Maybe something like this

## Identifiers and Constants

  Sometimes you need to interpolate an identifier or a constant value into a fragment,
  instead of a query parameter. The latter can happen if your database does not allow
  parameterizing certain clauses. For example:

      collation = "es_ES"
      fragment("? COLLATE ?", ^name, ^collation)

      limit = "10"
      "posts" |> select([p], p.title) |> limit(fragment("?", ^limit))

  The first example above won't work because `collation` needs to be quoted as an identifier. 
  The second example won't work on databases that do not allow passing query parameters
  as part of `limit`.

  You can address this by telling Ecto to treat these values differently than a query parameter:

      fragment("? COLLATE ?", ^name, identifier(^collation))
      "posts" |> select([p], p.title) |> limit(fragment("?", ^constant(limit))

  Ecto will make these values directly part of the query, handling quoting and escaping where necessary.

  > #### Query caching {: .warning}
  >
  > Because identifiers and constants are made part of the query, each different
  > value will generate a separate query, with its own cache.

@ftes
Copy link
Contributor Author
ftes commented Jun 23, 2025

Thanks, I like hour suggestions and copy & pasted them into this PR in case you want to merge.

@greg-rychlewski greg-rychlewski merged commit 18a633a into elixir-ecto:master Jun 23, 2025
7 checks passed
@greg-rychlewski
Copy link
Member

thanks for finding this :)

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

Successfully merging this pull request may close these issues.

2 participants
0