ecto

Ecto Query - Dates + Postgres Intervals + Query Interpolation

送分小仙女□ 提交于 2019-12-03 21:15:41
I would like to create an Ecto query that filters records in a children table by their age (i.e. "minimum age (months) -> maximum age (months)". One simple way to do this would be the Ecto date_add feature: from c in Child, where: c.birthday > datetime_add(^Ecto.DateTime.utc, -1, "month") The issue with this is that not all children will be on the same time zone, and certainly not all on Etc/UTC . This query would be pretty close, but not spot on (some would be off by a day). I've been trying to use PostgreSQL's interval functionality to make this query work. I can get it to work using an SQL

Ecto creating unique index failed for Mysql/Mariadb

被刻印的时光 ゝ 提交于 2019-12-03 16:56:42
I try to do the following migration: defmodule Shopper.Repo.Migrations.MakeNameUniqueShopper do use Ecto.Migration def change do create unique_index :shoppers, [:name] end end Also tried create unique_index :shoppers, [:name], name: :name_unique , create unique_index :shoppers, [:name], name: "name_unique" , and create index(:shoppers, [:name], unique: true) But they failed with similar error: [info] == Running Shopper.Repo.Migrations.MakeNameUniqueShopper.change/0 forward [info] create index shoppers_name_index ** (Mariaex.Error) (1071): Specified key was too long; max key length is 767 bytes

Using recursive CTE with Ecto

一笑奈何 提交于 2019-12-03 12:40:26
How would I go about using the result of a recursive CTE in a query I plan to run with Ecto? For example let's say I have a table, nodes, structured as so: -- nodes table example -- id parent_id 1 NULL 2 1 3 1 4 1 5 2 6 2 7 3 8 5 and I also have another table nodes_users structured as so: -- nodes_users table example -- node_id user_id 1 1 2 2 3 3 5 4 Now, I want to grab all the users with a node at or above a specific node, for the sake of an example let's choose the node w/ the id 8. I could use the following recursive postgresql query to do so: WITH RECURSIVE nodes_tree AS ( SELECT * FROM

Ecto - validate presence of associated model

人走茶凉 提交于 2019-12-03 10:45:40
How can one validate the presence of an associated model in Ecto ? schema "foo" do has_many: bar, Bar timestamps end @required_fields ~w(bar) # invalid Is there a way to do so ? And validate a min/max number of these fields ? José Valim There isn't anything yet. But you can run these validations yourself in your changeset function: def changeset(model, params) do model |> cast(...) |> validate_bar_association() end def validate_bar_association(changeset) do bar = changeset.model.bar cond do bar == nil -> add_error changeset, :bar, "No bar" length(bar) < 5 -> changeset true -> add_error

Ecto association with a condition

江枫思渺然 提交于 2019-12-03 10:45:12
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 comments and one to normal ones, how would I do it? So I want something like this: has_many :fancy_comments, MyApp.Comment, where: [type: 0] has_many :normal_comments, MyApp.Comment, where: [type: 1] This is not available in Ecto, there is a lengthy discussion about it on this GitHub issue . You could use a composable query for this: defmodule MyApp

Disable Elixir Ecto Debug output

蹲街弑〆低调 提交于 2019-12-03 10:05:48
Whatever in iex> or using mix run -e "My.code" when I run the mix project using ecto, the Ecto's Debugging Mechanism display a bunch of SQLs like below 16:42:12.870 [debug] SELECT a0.`id` FROM `account` AS a0 WHERE (a0.`account_name` = ?) ["71000000313"] (39.6ms)` ... When I dont need the debug output anymore, How can I turn it off, I cannot find anything about how to change ecto log level stuff. Thanks in advance. Your logging level is configured in your config/#{env}.exs files. If you look into config/prod.exs it most likely already has that level set to :info : config :logger, level: :info

Modify foreign key in Ecto

感情迁移 提交于 2019-12-03 06:38:53
问题 I have this original migration that have already been run and sent upstream: create table(:videos) do add :url, :string add :title, :string add :description, :text add :user_id, references(:users, on_delete: :nothing) timestamps end create index(:videos, [:user_id]) Now i wish to change the foreign key on user_id to cascade deletions, so that when a user is deleted all of his associated videos will also be deleted. I have tried the following migration: alter table(:videos) do modify :user_id,

How can I see the raw SQL generated for an Ecto.Query?

淺唱寂寞╮ 提交于 2019-12-03 06:36:43
问题 I have an Ecto.Query and a Repo , such that I can call Repo.all(query) and get results. However, the results are not what I expect. How can I see the raw SQL the Repo will generate from the Ecto.Query ? 回答1: You can use Ecto.Adapters.SQL.to_sql/3: iex> Ecto.Adapters.SQL.to_sql(:all, Repo, Post) {"SELECT p.id, p.title, p.inserted_at, p.created_at FROM posts as p", []} The query can be any struct that implements the Ecto.Queryable protocol like Post above(which is a module that imports Ecto

How to rollback, reset, or drop Ecto test database?

走远了吗. 提交于 2019-12-03 05:29:00
Usually mix.test cleans the test database, but it is not working. It may be because I was playing around with making a users schema, but didn't want to use what I made so I got rid of it. I then started over and made a new schema for users which was different from the first. When I tried to run mix test again, there was an error that some fields did not exist which should have been there with the new schema. Katherine You can access the test database by using MIX_ENV=test followed by a command such as mix do ecto.drop , mix ecto.reset or mix ecto.rollback . In this particular case, I used: MIX

Is there a Phoenix equivalent to Rails Console

邮差的信 提交于 2019-12-03 03:23:13
问题 I'm just learning Phoenix and Elixir and I'm coming from Ruby/Rails where I work in the REPL using pry to inspect my database and application state. I'm trying to figure out how to interact with my database and models in a Phoenix app. I'm aware of iex , but I don't know how to use it inspect my app's database from the repl. Do I need to connect to it with ecto each time from the repl? Is there a rails console equivalent. I've checked the Phoenix docs, Elixir Dose, and the Ecto repo, but can