Because this query performs a JOIN, the number of results returned by the database is POSTS * COMMENTS, which Ecto then processes and associates all comments into the appropriate post.įinally, Ecto also allows data to be preloaded into structs after they have been loaded via the Repo.preload/3 function: Repo. ![]() The example above will now perform a single query, finding all posts and the respective comments that match the criteria. all from p in Post, join : c in assoc ( p, :comments ), where : c. For example, imagine both posts and comments have votes and you want only comments with more votes than the post itself: Repo. It is also possible to preload associations using joins while performing more complex queries. This is often the most efficient way of loading associations from the database (even if two queries are performed) because we need to receive and parse only POSTS + COMMENTS results. The example above will perform two queries: one for loading all posts and another for loading all comments. Now all posts will be fetched from the database with their associated comments. One of the benefits of defining associations is that they can be used in queries. You can think of the schema that calls has_* as the parent schema and the one that invokes belongs_to as the child one. The difference between has_one/3 and belongs_to/3 is that the foreign key is always defined in the schema that invokes belongs_to/3. For example, you could think of a metadata association where “Post has one metadata” and the “Metadata belongs to post”. Similar to has_many/3, a schema can also invoke has_one/3 when the parent has at most one child entry. ![]() First create the two tables in migrations: create table ( :posts ) do add :title, :string add :body, :text timestamps end create table ( :comments ) do add :post_id, references ( :posts ) add :body, :text timestamps endĮach comment contains a post_id column that by default points to a post id.Īnd now define the schemas: defmodule do use Ecto.Schema schema "posts" do field :title field :body has_many :comments, timestamps end end defmodule do use Ecto.Schema schema "comments" do field :body belongs_to :post, timestamps end endĪll the schema definitions like field, has_many and others are defined in Ecto.Schema. You can learn more about those in Ecto docs.Īssociations in Ecto are used when two different sources (tables) are linked via foreign keys.Ī classic example of this setup is “Post has many comments”. This article expects basic knowledge Ecto, particularly how repositories, schema and the query syntax work. ![]() At the end, we give a more complex example that uses Ecto associations to build nested forms in Phoenix. This blog post aims to document how to work with associations in Ecto, covering how to read, insert, update and delete associations and embeds. Working with Ecto associations and embeds
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |