Writing Queries with Schemas
When we looked at queries in the last chapter, we deliberately chose to write them without schemas. This is still a good choice in some circumstances, but schemas provide some helpful shortcuts and we promised that we’d revisit them later. Now’s the time. In this section, we’ll start writing queries that work with schemas, and we’ll also consider when it’s best not to.
Converting a Schema-less Query
Let’s look at a query we ran in the last chapter:
| artist_id = "1" |
| q = from "artists", where: [id: type(^artist_id, :integer)], |
| select: [:name] |
| Repo.all(q) |
| #=> [%{name: "Miles Davis"}] |
As you may recall, artist_id is initialized as a string, ...
Get Programming Ecto now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.