Questions tagged [ecto]

Ecto is a domain specific language for writing queries and interacting with databases in Elixir.

ECTO is a domain specific language for writing queries and interacting with databases in . Ecto, with database specific adapter, can support , , , and .

1773 questions
22
votes
2 answers

Preloading Ecto Associations by default

Is there any way to preload ecto associations without explicitly using preload:? Something like an option in the schema? schema "gadgets" do field :foo, has_many :bars, Myapp.Bar, preload: true end I'm doing something like Repo.get(Gadget, id) …
Dania_es
  • 1,026
  • 1
  • 10
  • 20
21
votes
2 answers

How to check if a association is not loaded?

What function can I use to check if a association is already loaded? It would be nice to check if a association is loaded instead of trying to use it and getting Ecto.Association.NotLoaded error.
Teo Choong Ping
  • 12,512
  • 18
  • 64
  • 91
21
votes
1 answer

Why I got #Ecto.Association.NotLoaded?

I have teams and each team has users, so there is a join table to link users to teams as its a many to many relation, here is my models: defmodule App.Team do use App.Web, :model schema "teams" do field :owner_id, :integer has_many…
simo
  • 23,342
  • 38
  • 121
  • 218
20
votes
1 answer

How to compare dates with Elixir and Ecto

I have an instance of Ecto.DateTime that I get from a database column and I need to check if this date is more than 5 minutes in the past. What is the best way to do that? To make it more clear, this is what I need in ruby -> updated_at < (Time.now…
NoDisplayName
  • 15,246
  • 12
  • 62
  • 98
19
votes
6 answers

How to use Postgres' enumerated type with Ecto

With PostgreSQL, we can do something like this: CREATE TYPE order_status AS ENUM ('placed','shipping','delivered') From Ecto's official doc, there is no native type to map the Postgres' enumerated type. This module provides a custom type for…
18
votes
3 answers

Elixir + Ecto: How to do WHERE NOT IN [array]?

I am trying to look for all Users that don't have a certain string element in their match_history field. I took a guess with this: matched_user = User |> where([u], ^device_id not in u.match_history) |> limit(1) |> VideoChat.Repo.one But it seems to…
bigpotato
  • 26,262
  • 56
  • 178
  • 334
18
votes
5 answers

Why does Phoenix (ecto/Postgresx) fail to Connect in dev

I am beginning my Elixir/Phoenix journey and having some trouble with my postgres connection. When I start up my server I get: $ mix phoenix.server [error] Postgrex.Protocol (#PID<0.214.0>) failed to connect: ** (Postgrex.Error) tcp connect:…
Will
  • 8,102
  • 5
  • 30
  • 32
18
votes
2 answers

Phoenix Ecto how to handle NoResultsError

In my Phoenix JSON API I am getting an Ecto NoResultsError when I request an object that doesn't exist in the database. I want my JSON API to return a null along with a 404 error. How would I do this? Currently I pretty much have a default generated…
Josh Petitt
  • 9,371
  • 12
  • 56
  • 104
18
votes
1 answer

Ecto model `undefined function: ` when working with from macro ***in iex***

I am having this issue with Ecto project. None of the queries are working. I did fair a bit of googling and github issues search. There are few but unrelated to my problem. This question was kicked off from this one…
Andrew Shatnyy
  • 1,528
  • 14
  • 19
18
votes
1 answer

how to use RethinkDB with Phoenixframework?

Having just arrived to Elixir/Phoenix I want to use RethinkDB instead of PostgreSQL but I only find documentation/examples on PostgreSQL (which seems to be the default/official database). There is a very good package from Hamiltop (Rethinkdb-elixir)…
Paulo Janeiro
  • 3,181
  • 4
  • 28
  • 46
17
votes
1 answer

Ecto: How to preload records with selecting another joined columns

Are there any ways to preload records by selecting another joined columns? # table structure # User 1---* Post 1---* PostTag *---1 Tag # extract definition of scheme scheme "posts" do ... has_many :post_tags, PostTag has_many :tags, [:post_tags,…
KONDO Daisuke
  • 304
  • 2
  • 8
17
votes
1 answer

How to add field in existing table phoenix

So I'm a newbie in phoenix. I created a simple api in my phoenix project the problem is that i want to add another field in my todo.ex I want to add an author field in my todo.ex FROM defmodule TodoApi.Todo do use TodoApi.Web, :model schema…
code.cycling
  • 1,246
  • 2
  • 15
  • 33
17
votes
1 answer

Elixir Ecto: How to set a belongs_to association in a changeset

I am a bit stuck in how to actually set an association with a changeset. I have this code in my model: defmodule MyApp.MemberApplication do use MyApp.Web, :model use Ecto.Schema use Arc.Ecto.Schema alias MyApp.Repo alias…
Ole Spaarmann
  • 15,845
  • 27
  • 98
  • 160
17
votes
2 answers

Ecto association with a condition

Let's say I have two models, Post and Comment and the comment model can be 1 out of 2 types, normal and fancy which is defined by the column type in the comments table. Now I want to add 2 associations on my Post model, where one refers to fancy…
NoDisplayName
  • 15,246
  • 12
  • 62
  • 98
17
votes
2 answers

Inserting associated models in Ecto

I'm trying to insert an invoice struct along with its associated invoice items. I'm able to insert the invoice data, and call an anonymous function to validate, cast, and insert each item. Since insert/2 does not produce a return, how can I get…
Dania_es
  • 1,026
  • 1
  • 10
  • 20