ecto

How do interpolation fields in Ecto queries?

不问归期 提交于 2021-02-08 11:14:52
问题 The Ecto documentation shows how to do interpolation values. But I need dynamic fields in my queries. I have dozens of fields and write queries for each of them does not seem cohesive. defmodule Hedone.SearchController do use Hedone.Web, :controller alias Hedone.User def idade(conn, %{"idade+" => maior, "idade-" => menor, "campo" => campo}) do IO.inspect campo query = from u in User, where: u.campo > ^maior and u.campo < ^menor, select: u.name pesquisa = Repo.all query IO.inspect pesquisa

Using fragment & group_by together with postgres & ecto?

假如想象 提交于 2021-01-28 09:33:49
问题 I'm struggling to get this to work with Postgres and Ecto. The query below works fine without the group_by, but I need to group on the fragment field, which it can't seem to see. Any idea what's wrong with it? def query_clicks do from(Click) |> select( [c], [ fragment("date_trunc('hour',?) as hour", c.inserted_at), c.link_id] ) |> group_by([c], c.hour) |> Repo.all end Result: iex(1)> recompile; Shortr.LinkContext.query_clicks [debug] QUERY ERROR source="clicks" db=1.2ms queue=4.9ms SELECT

Using fragment & group_by together with postgres & ecto?

删除回忆录丶 提交于 2021-01-28 09:30:46
问题 I'm struggling to get this to work with Postgres and Ecto. The query below works fine without the group_by, but I need to group on the fragment field, which it can't seem to see. Any idea what's wrong with it? def query_clicks do from(Click) |> select( [c], [ fragment("date_trunc('hour',?) as hour", c.inserted_at), c.link_id] ) |> group_by([c], c.hour) |> Repo.all end Result: iex(1)> recompile; Shortr.LinkContext.query_clicks [debug] QUERY ERROR source="clicks" db=1.2ms queue=4.9ms SELECT

Elixir comprehension returning a star character '*'

允我心安 提交于 2021-01-05 06:24:10
问题 I have a list of Persona models being returned in p.followings and I want to extract the followed_id field from this list of models into a separate list. p.followings returns... [ %Poaster.Personas.Following{ __meta__: #Ecto.Schema.Metadata<:loaded, "followings">, followed: %Poaster.Personas.Persona{ __meta__: #Ecto.Schema.Metadata<:loaded, "personas">, background_image_url: nil, bio: "ASDF", followings: #Ecto.Association.NotLoaded<association :followings is not loaded>, id: 42, inserted_at:

How to retrieve id from previous inserted table key by Ecto Multi

扶醉桌前 提交于 2020-11-29 19:21:27
问题 I would like to retrieve id from previous inserted table primary key by Ecto Multi. At first, I insert to A main table. then B details table needs A.id. I tried following code. Multi.new() |> Multi.insert(:insert, main) |> Multi.insert_all(:insert_all, B, details) |> Repo.transaction() However I have no idea how to retrieve A.id for insert table B. What I should do for it? 回答1: You can do something like the following example which creates a new User record and a new Email record (where the

How to retrieve id from previous inserted table key by Ecto Multi

走远了吗. 提交于 2020-11-29 19:20:16
问题 I would like to retrieve id from previous inserted table primary key by Ecto Multi. At first, I insert to A main table. then B details table needs A.id. I tried following code. Multi.new() |> Multi.insert(:insert, main) |> Multi.insert_all(:insert_all, B, details) |> Repo.transaction() However I have no idea how to retrieve A.id for insert table B. What I should do for it? 回答1: You can do something like the following example which creates a new User record and a new Email record (where the

How do I avoid Ecto matching incorrectly when comparing binary IDs with == in a Latin database?

浪尽此生 提交于 2020-07-10 03:25:39
问题 I've come across a problem with my Elixir Ecto MySQL database. We've found that the character set is latin, which is probably part of the problem. I have an object in the database with the id: field set to "3f0c7254-693f-4574-3f3f-256c3f3f5224". If I do a query on the database using this block of Elixir to generate the query: params |> Enum.reduce(@schema, fn {k, v}, query -> where(query, [b], field(b, ^k) == ^v) end) I can pass id: "b30c7254-69d1-4574-a489-256cd9c45224" to the query (as

What do the on_delete options in Ecto.Migrations.references/2 do?

扶醉桌前 提交于 2020-05-28 09:59:26
问题 The Ecto documentation describes the options available to references/2 , but does not document what those options do. The options available are: :nothing :delete_all :nilify_all :restrict What do they do? 回答1: This is actually a SQL question at root. https://github.com/elixir-ecto/ecto_sql/blob/52f9d27a7ad86442f442bad2f7ebd19ba09ddc61/lib/ecto/adapters/myxql/connection.ex#L902-L905 The PostgreSQL documentation outlines these options clearly: :nothing - if any referencing rows still exist when

Using a Repo in an Ecto migration

守給你的承諾、 提交于 2020-05-15 09:42:27
问题 I've got an Ecto migration where I'd like to modify some columns but also migrate some data. For example: import Ecto.Query defmodule MyApp.Repo.Migrations.AddStatus do alter table(:foo) do add(:status, :text) end foos = from(f in MyApp.Foo, where: ...) |> MyApp.Repo.all Enum.each(foos, fn(foo) -> # There's then some complex logic here to work # out how to set the status based on other attributes of `foo` end) end Now, the problem here is that by calling MyApp.Repo.all the migration