![]() ![]() Plus we have a pattern to follow that’s a little more extensible moving forward, should we need to introduce event logs for different types of events.Įcto.Query#select_merge/3 allows us to populate a virtual field directly within a select query, giving us all kinds of flexibility when it comes to designing schemas and composing queries. We get exactly the data we need without adding any new associations, keeping our schemas decoupled. Most examples will use the keyword-based syntax, the macro one will be explored in later sections. Ecto queries come in two flavors: keyword-based and macro-based. admitter_name "Albus Dumbledore"īy adding a virtual field and populating it with select_merge, we end up with a much lighter-weight solution. Queries are used to retrieve and manipulate data from a repository (see Ecto.Repo ). defmodule do use Ecto.Schema import Ecto.Query, only : alias Registrar.Admission alias schema "admission_events" do field ( :action, :string ) field ( :admission_id, :integer ) field ( :admitter_uuid, Ecto.UUID ) field ( :occurred_at, :naive_datetime ) end def for_admission ( query \\ AdmissionEvent, % Admission ] iex> event = List. So if we did a straight-forward select query, we’d end up with the admission events we need to populate the table, but not the admitters’ full names. The problem here is the admitter’s full name lives on User, which isn’t currently associated with AdmissionEvent. …and just for the sake of completeness, a super simple Admission schema: defmodule Registrar.Admission do use Ecto.Schema schema "admission" do field ( :admittee_uuid, Ecto.UUID ) field ( :admitter_uuid, Ecto.UUID ) field ( :admitted_at, :naive_datetime ) end end …and a User schema that looks like this: defmodule Registrar.User do use Ecto.Schema schema "users" do field ( :uuid, Ecto.UUID ) field ( :full_name, :string ) end end To start with, we have an AdmissionEvent schema that looks like this: defmodule do use Ecto.Schema schema "admission_events" do field ( :action, :string ) field ( :admission_id, :integer ) field ( :admitter_uuid, Ecto.UUID ) field ( :occurred_at, :naive_datetime ) end end but Ecto is throwing a (Ecto.QueryError) cannot preload associations in subquery in query. Say you work at a school with an admissions department, and you’ve been tasked with displaying an event log showing all the events related to a given admission, organized into three columns: 1) date, 2) action taken, and 3) who the action was taken by. Let’s run through an example to see it in action. You can accomplish that same end result in a single query expression with Ecto.Query#select_merge/3. Happily, today I learned there’s a better way. In the past, whenever this happened, I’d usually spin up something I wasn’t totally satisfied with – maybe updating the schema(s), breaking it up into multiple queries, or building a multi-select statement if I was feeling fancy. , % Friends.Working with Elixir and Ecto, I’ve run into scenarios where I needed to retrieve data from a table plus maybe a field or two from an unassociated table. Below, we’ll get the movie with an ID of 1: iex> alias Friends. It returns nil if no such record is found. ![]() It returns a struct describing the record found, if any. This function requires two arguments: a “queryable” data structure and the ID of a record to retrieve from the database. We can use the Repo.get/3 function to fetch a record from the database given its ID. We can perform simple queries directly against our Friends.Repo with the help of a handful of functions. ![]() Recall that a “repository” in Ecto maps to a datastore such as our Postgres database.Īll communication to the database will be done using this repository. In this lesson, we’ll continue building off the Friends app and the movie-cataloguing domain we set up in the previous lesson. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |