kostonstyle
Ecto the pin operator
Hi all
When I want to query something from db, for exmaple
query = from p in EctoBlog.Post, where: p.id == ^post_id
Why do I need here the pin operator?
Thanks
Marked As Solved
benwilson512
This is simply a choice of how Ecto wanted to make its api. It helps make it clear when a term refers to a table, and when it refers to an injected value. For example consider the following example:
u = %User{id: 1}
from u in User, where: u.id == ^u.id
The pin operator makes it clear that the right hand side of the == is referring to the external u.
Also Liked
KronicDeth
Think of it as a safe interpolation operator: the Ecto query doesn’t see your variables outside the from macro call unless you tell it to look outside itself. You using the ^ is marking that the variable is outside the DSL language of the macro to separate the variables like p that represent a table. This always the macro to detect errors and typos at compile time as it can be sure if a variable is in the DSL or from the scope.
Last Post!
KronicDeth
Think of it as a safe interpolation operator: the Ecto query doesn’t see your variables outside the from macro call unless you tell it to look outside itself. You using the ^ is marking that the variable is outside the DSL language of the macro to separate the variables like p that represent a table. This always the macro to detect errors and typos at compile time as it can be sure if a variable is in the DSL or from the scope.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









