Questions tagged [ecto]

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

0
votes
1answer
18 views

put_assoc complains of wrong type

I have Ecto schemas representing a directed graph -- there is a Node schema with a self-referential many-to-many relation through an intermediate NodeEdge schema. i.e. schema "nodes" do field :...
0
votes
1answer
42 views

Inserting foreign key from building association with given schema in single transaction

I have a schema exchange_accounts and user_profile user_profile has a foreign_key column exchange_accounts_id which is referenced from exchange_accounts schema. I want to insert data in user_profile ...
0
votes
1answer
26 views

How to use ANY operator with array field with Ecto?

How to find all posts with a tag 'apple' ? schema "posts" do field :title, :string field :content, :string field :tags, {:array, :string} timestamps() end With postgresql it ...
0
votes
1answer
36 views

Weird error which shows up *sometimes* in tests in Elixir using Ecto

Error: 14:18:04.259 [error] GenServer MyApp.Receiver.Handlers terminating ** (Ecto.Query.CastError) deps/ecto/lib/ecto/repo/queryable.ex:382: value `1234` in `where` cannot be cast to type :string ...
1
vote
0answers
27 views

Interpolating list of UUIDs into a raw SQL query (can't use fragment)

I'm having issues interpolating a list of UUIDs to perform an in query. Ecto.Adapters.SQL.query!(Repo, " WHERE some_id IN ($1) ", [some_list_of_ids]) This gives me an error ** (ArgumentError) ...
1
vote
0answers
23 views

For Distillery 2.x, how to fetch runtime configuration values in a release task?

So basically I need some Ecto repository information (call it "Sample.Repo") to start the repo in a release task for Ecto 3. It's configured in a runtime config with a custom json config provider as ...
1
vote
1answer
26 views

Ecto - how to asynchronously update a batch of records from one table with respect to another table

I have two tables: assignments and reports. A report is made to keep stats on assignments. So, every time an assignment is created or updated, all reports must be indiscriminately updated. I do ...
0
votes
1answer
45 views

how to write this where clause in Ecto

I have to filter an index that could accept in some cases comma separated values. Im working with Ecto, without phoenix (with cowboy). my index will support some search like this /users?ids=1,2,3&...
1
vote
1answer
15 views

Elixir / Phoenix / Ecto: How to customize SQL query log format?

In my Phoenix/Ecto apps, when I set log level to :debug, I see helpful log entries for each SQL query issued by Ecto, like this: [debug] QUERY OK source="users" db=1.9ms SELECT u0."id", u0."full_name"...
1
vote
0answers
46 views

Ecto put_assoc changeset is correct, but Repo.update gives StaleEntryError exception

I am trying to update some data in the mysql table. I am using Ecto to perform database operations. The User table has an association with ItemsPurchased, the has_many association and belongs_to are ...
1
vote
1answer
43 views

Storing calculated column to use in order_by and select

So I'm doing a PostGis calculation. I have a User and a Place model. Each have a :geom, and Place has a virtual distance field. I want to order by distance, as well as select it. I have it working ...
0
votes
2answers
51 views

composite unique constraint error while updating the changeset

I have a schema two_fa_details where answer and question_id are the fields and both are unique together.. Now when I am trying to insert data into it first it gets inserted but updating it next time ...
0
votes
1answer
34 views

Ecto model query to return related objects through a one to one mapping

I'm having difficulty in trying to locate in the documentation on how to do the following Ecto query. I have a model events which has a one to one mapping to a customer through event.customer_id. In ...
0
votes
1answer
40 views

Many to many association preload error in Ecto

I am learning Ecto and was trying out many to many relationship. I am using MySQL DB, schemas are like this User Schema @primary_key {:id, :binary_id, autogenerate: true} schema "users" do ...
1
vote
1answer
24 views

How to define a transient field in Ecto?

How do I define a transient field in Ecto, i.e. a field that will not be persisted along the rest of the schema? I searched everywhere including the documentation and can't find anything referring to ...
0
votes
1answer
45 views

Reading an html file in an ecto migration

I'm trying to read an html file from my priv/repo/templates and insert it as a string value to a table: def change do consultant_engagement_html = File.read!(Application.app_dir(:enterprise, "priv/...
0
votes
1answer
32 views

Ecto JOIN complications for append-only table query

I'm trying to query an Ecto table with append-only semantics, so I'd like the most recent version of a complete row for a given ID. The technique is described here, but in short: I want to JOIN a ...
0
votes
1answer
35 views

Building association between two tables before inserting

I have tables user_profile and roles.There exit a foreign key column roles_id in user_profile referencing roles table. I have to build_association between both tables before inserting the data into ...
-1
votes
0answers
27 views

Ecto - finding records where a join table doesn't exist (given a specific input ID)

So I have a basic two tables (User, Room), and a join table (UserRoom). I want to be a able to query "non-subscribed" Rooms for a given user. I've gotten this far with my query def ...
1
vote
1answer
39 views

How to store two dimension array of string in Elixir Ecto. How to put matching length to list?

How do I save 2d lists into postgres? Or put matching lengths to lists? I tried this in my migration.. add :checkpoints, {:array, :string} and schema.. field :checkpoints, {:array, {:array, :...
0
votes
1answer
34 views

Storing module names inside map in database

I am creating a notification service for an IOT app using Elixir/Ecto/Phoenix. A sensor in this app can have multiple values e.g. temperature: ["temp"], accelerometer: ["ax", "ay", "az"] To create ...
0
votes
0answers
33 views

Using `cast_assoc` to associate records on different schemas

I'm trying to use cast_assoc to associate records that exist on different schemas. In the code below, Organization exists in a tenant schema (e.g. "tenant_2837.organizations"), whereas Workspace ...
0
votes
1answer
26 views

(ArgumentError) could not generate inputs for <association>

Having some issues with rendering changeset errors on a nested form. Form rendering: def new(conn, _) do changeset = Organizations.Organization.create_organization_changeset(%Organization{...
1
vote
1answer
65 views

Elixir Repo.insert_all/2 and unique constraint

I've got a rather interesting problem today. I'm trying to implement a bulk insert from CSV file into my database using the Ecto function Repo.insert_all/2 however, one thing bothers me. The problem ...
0
votes
1answer
25 views

Ecto query to grab all values that satisfy all values in array_aggregator not just any?

Wonder if someone can help me with an array aggregator issue I’ve got a query that does a join using a joining table then it filters down all values that are inside a given array and filters out ...
0
votes
1answer
23 views

Ecto query unique through a belongs_to association?

I have two models Event and Customer. The relationship is that an Event belongs_to a Customer, which can has_many Events. Now, I need to return the count of unique Customers by way of Events, which ...
0
votes
0answers
54 views

Async tasks returning no results

I have around 10-15 ecto queries which I want to run async in my API code. I am using Task.async and Task.yeild_many Following is the code for async task - def get_tasks() do task_1 = Task....
1
vote
1answer
26 views

Postgrex - query returning an ambiguous column reference error

I have the following queries: def user_contacts(provider_id, filter) do query = from( u in User, preload: [:groups], where: u.provider_id == ^provider_id and u.type != "...
0
votes
1answer
54 views

Why does dumping DateTime to :utc_datetime_usec with a precision less than 6 fail?

I am wondering why I can't dump a DateTime with a precision less than 6? Why can't we just fill up with zeros? Example iex> {:ok, datetime, _} = DateTime.from_iso8601("1970-01-01T00:00:00Z") {:ok,...
0
votes
1answer
27 views

httpoison cannot access elasticsearch from webapp in a docker environment

When trying to bring up a project with a webapp (using elixir/ecto as backend language),a postgres database, elasticsearch, and kibana using following docker-compose.yaml file: version: '3' services: ...
1
vote
1answer
34 views

Start Ecto for Mix task (Mix.EctoSQL.ensure_started not working anymore)

I know this question has been asked already and the answers were always individual answers. However I've seen Mix.EctoSQL.ensure_started mentioned quite a bit and it seems as if this is the accepted ...
0
votes
0answers
36 views

Insert_or_update/1 on ecto repo not updating existing entry

I have an issue with updating existing entries upon receiving a POST request. The desired functionality is that a new entry gets written to the database if none exists. Otherwise a counter on the ...
0
votes
1answer
31 views

Is it bad practice to use raw-text SQL queries with Phoenix?

I have a query that uses "AS" and "SUM". I am unable to find any applications of this in the Phoenix documentation, so I am starting to wonder if I should just use raw SQL queries. I am new to Phoenix,...
0
votes
1answer
34 views

Preload has_one association in a case insensitive manner

We're currently in the process of migrating from Ruby/Rails/MariaDB to Elixir/Phoenix/MariaDB and I've come across an issue when preloading an association in Ecto that we didn't see with ActiveRecord. ...
0
votes
1answer
44 views

Postgres - Insert timestamps in an INSERT INTO query while selecting values from tables?

I'm trying to run a migration in a Phoenix application, but Postgrex returns the following error: null value in column "inserted_at" violates not-null constraint The query that generated that error ...
1
vote
1answer
76 views

Elasticsearch service from docker image does not connect to webapp or kibana

I have a docker-compose.yml file which declares webapp, postgres database, a two node elasticsearch, and a kibana container. version: '3' services: webapp: build: context: ../../../ ...
0
votes
1answer
30 views

Foreign value getting inserted as blank in database. how to resolve this?

I am trying to insert data in a table where one of the column is foreign key.I have created the association between tables but foreign key is getting inserted as blank. I have a schema for user_type ...
0
votes
1answer
21 views

How to add prefix to `join_through` table in `many_to_many` `assoc` at the Repo.*() level?

I'm trying query a many_to_many relationship with a prefix as follows: Student |> join(:left, [s], t in assoc(s, :teachers)) |> Repo.all(prefix: "my_prefix") which results in a PostgreSQL ...
0
votes
0answers
47 views

Ecto fuzzy match on multiple words

In my DB (PostgreSQL) I added the fuzzystrmatch extension and have been trying to use the levenshtein function to match items. It does not work how I would like it to when the columns are strings of ...
0
votes
1answer
35 views

Ecto query with fragment

I have the following query in my pipeline: where([e], fragment("?->>? = ?", e.fields, ^field_name, ^field_value)) e in this case stands for event. What is the first parameter of the fragment ...
0
votes
1answer
39 views

Elixir Ecto Phoenix - Comparing string date format “YYYY-MM-DD” with #DateTime<2017-12-01 13:31:14.987000Z>

I'm trying to implement a date range in my Ecto query where the date ranges are string dates of format "YYYY-MM-DD", and the corresponding created_at date in the database is in #DateTime<2017-12-01 ...
0
votes
0answers
34 views

validate_required does not set changeset as invalid if field missing

I have a changeset called by an update (PUT) method in a phoenix application. The validate_required function is not working as I expect it to. If no values are sent in the PUT request the changeset is ...
0
votes
1answer
37 views

Can't seed with belongs_to association in Phoenix / Ecto

I have a Team and Game table like so : schema "teams" do field :name, :string has_many :games, LvdnApi.Fixtures.Game timestamps() end schema "games" do field :away_team_score, :...
0
votes
1answer
37 views

Ecto looks for field_id instead of field

I am trying to setup a Phoenix API with GraphQL. I have a Team table and a Game Table in my DB like this: schema "teams" do field :name, :string has_many :games, LvdnApi.Fixtures.Game ...
0
votes
0answers
59 views

Elixir Ecto Multi insert_all

I'm new to using ecto.multi and I'm stuck on how to use the multi.insert_all function. paydata_changeset = Paydata.changeset(%Paydata{}, paydata_params) multi = Multi.new() |> Multi.insert(:...
1
vote
0answers
35 views

Wrong group query when trying to build dynamically

I am trying to working on a query builder and the example/content is just imaginary. query = from en in Data.InfectionRevision query = from q in query, select: map(q, [:is_complete, :is_active]) ...
0
votes
1answer
42 views

How to query for empty array in jsonb PostgreSQL using Elixir / Ecto

I have an Ecto schema with embeds_many defined like this: schema "rounds" do embeds_many :growth_cycles, SomeModule.GrowthCycle, on_replace: :delete end This translates to a jsonb field in ...
0
votes
1answer
26 views

Can Ecto cast a list to an integer array in a PostgreSQL query?

Using PostgreSQL and Ecto, I'm trying to select a list of rows by id in the order specified. In psql, this query gets items with ids 4, 2, 1, and 4 again, in that exact order. SELECT items.id, items....
0
votes
1answer
45 views

How to use Ecto.Migration constraint to enforce only one field

I have a table called Accounts that must have either a user or organization that "owns" it. I want to enforce a constraint that enforces that EITHER user_id or organization_id has been provided, but ...
2
votes
1answer
41 views

Blank structs for selecting records with left joined subquery

I have a few simiple Ecto structs: defmodule MyApp.ForumCategory do use MyApp.Schema schema "forum_categories" do field :name, :string field :last_topic, :map, virtual: true field :...