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

Update querying_basics.md #2979

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

wawashra
Copy link

@wawashra wawashra commented Feb 28, 2024

Edit the last example to use subquery rather than ^, because it has an error

iex(76)> movies = from m in Movie, where: [title: "good will hunting"]
#Ecto.Query<from m0 in Friends.Movie, where: m0.title == "good will hunting">
iex(77)>
nil
iex(78)> characters_query = from c in Character,
...(78)> join: ^movies,
...(78)> on: [id: c.movie_id], # keyword list
...(78)> where: c.name == "Wade Watts",
...(78)> select: {m.title, c.name}
** (Ecto.Query.CompileError) unbound variable m in query. If you are attempting to interpolate a value, use ^var
(ecto 3.11.1) expanding macro: Ecto.Query.select/3
iex:78: (file)
(ecto 3.11.1) expanding macro: Ecto.Query.from/2
iex:78: (file)

To verify I used the same format to make a query on my local DB, using the movie title, and verifying the characters of the movie,

The results are

`iex(71)> movies = from m in Movie, where: [title: "good will hunting"]
#Ecto.Query<from m0 in Friends.Movie, where: m0.title == "good will hunting">
iex(72)>
nil
iex(73)> characters_query = from c in Character,
...(73)> join: m in subquery(movies),
...(73)> on: [id: c.movie_id], # keyword list
...(73)> select: {m.title, c.name}
#Ecto.Query<from c0 in Friends.Character,
join: m1 in subquery(from m0 in Friends.Movie,
where: m0.title == "good will hunting"),
on: m1.id == c0.movie_id, select: {m1.title, c0.name}>
iex(74)>
nil
iex(75)> Repo.all(characters_query)

10:45:53.327 [debug] QUERY OK source="characters" db=15.8ms idle=1525.6ms
SELECT s1.title, c0.name FROM characters AS c0 INNER JOIN (SELECT sm0.id AS id, sm0.title AS title, sm0.tagline AS tagline, sm0.inserted_at AS inserted_at, sm0.updated_at AS updated_at FROM movies AS sm0 WHERE (sm0.title = 'good will hunting')) AS s1 ON s1.id = c0.movie_id []
[
{"good will hunting", "Will Hunting"},
{"good will hunting", "Sean Maguire"},
{"good will hunting", "Skylar"}
]`

Edit the last example to use subquery rather than ^, because it has an error
@wawashra wawashra requested a review from a team as a code owner February 28, 2024 08:46
@wawashra wawashra marked this pull request as draft May 25, 2024 17:36
@wawashra wawashra marked this pull request as ready for review May 25, 2024 17:36
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.

None yet

1 participant