kostonstyle

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

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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

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

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.

Where Next?

Popular in Questions Top

joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40082 209
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement