ecto

Ecto “left IN right” query using a fragment

纵饮孤独 提交于 2019-12-10 17:46:47
问题 I would like to query a jsonb field using postgres IN operator (with Ecto library) This code work with a simple = operator: from a in query, where: fragment("?->>'format' = ?", a.properties, "foo") But I cannot make any of these attempts to work: from a in query, where: fragment("?->>'format' IN ?", a.properties, ["foo", "bar"]) from a in query, where: fragment("?->>'format' IN (?)", a.properties, ["foo", "bar"]) from a in query, where: fragment("?->>'format' IN ?", a.properties, "('foo',

Fix protocol Ecto.Queryable not implemented error

烂漫一生 提交于 2019-12-10 16:33:25
问题 I'm new to using Ecto and Elixir and I've come across an error that I can't explain. My code looks just like the example in the Ecto README. Here are my modules for the Ecto Model and Query defmodule Registration do use Ecto.Model schema "registrations" do field :user_id, :string field :created_at, :datetime, default: Ecto.DateTime.local field :updated_at, :datetime, default: Ecto.DateTime.local end end defmodule RegistrationQuery do import Ecto.Query def by_user(user_id) do query = from r in

Ecto query and custom MySQL function with variable arity

孤者浪人 提交于 2019-12-10 12:43:33
问题 I want to perform a query like the following one: SELECT id, name FROM mytable ORDER BY FIELD(name, 'B', 'A', 'D', 'E', 'C') FIELD is a MySQL specific function, and 'B', 'A', 'D', 'E', 'C' are values coming from a List. I tried using fragment, but it doesn't seem to allow dynamic arity known only in the runtime. Except going full-raw using Ecto.Adapters.SQL.query , is there a way to handle this using Ecto's query DSL? Edit: Here's the first, naive approach, which of course does not work: ids

Ecto Model - subquery in select

断了今生、忘了曾经 提交于 2019-12-10 10:19:26
问题 I need to make this SQL query with Ecto: SELECT users.*, (select count(0) from money_transactions where from_id = users.id AND created_at > '2016-1-25 0:00:00.000000') as money_transactions_today_db FROM "users" WHERE "users"."client_token" = '123' I try to do something like this but it doesn't work: query = from users in Like4uElixir.User, where: users.client_token in ^tokens, select: {users, (from money_transactions in Like4uElixir.MoneyTransaction, where: money_transactions.from_id ==

Default datetime with Ecto & Elixir

最后都变了- 提交于 2019-12-10 03:54:31
问题 I've just started working Elixir & Phoenix today, i am trying to add Ecto as a mapper, but i'm having some trouble using time. This is my model. schema "users" do field :name, :string field :email, :string field :created_at, :datetime, default: Ecto.DateTime.local field :updated_at, :datetime, default: Ecto.DateTime.local end I'm trying to set the created_at and updated_at per default, but when i try to compile this, i get the following error. == Compilation error on file web/models/user.ex =

cannot invoke remote function inside match : Foreach loop

社会主义新天地 提交于 2019-12-10 03:31:18
问题 I'm trying to set some property of User model inside a for-each loop, But I keep getting following error cannot invoke remote function x.token/0 inside match (elixir) src/elixir_fn.erl:9: anonymous fn/3 in :elixir_fn.translate/3 (stdlib) lists.erl:1353: :lists.mapfoldl/3 (elixir) src/elixir_fn.erl:14: :elixir_fn.translate/3 Method: Enum.each(users, fn(user) -> user.token = Comeonin.Bcrypt.hashpwsalt(to_string(user.id)) end) 回答1: There are a few issues here. The = operator is the match

Making a field unique in ecto

天大地大妈咪最大 提交于 2019-12-09 14:00:35
问题 How to make a field unique in ecto? I thought it's the same as the active record in Ruby, but it seems it isn't 回答1: You want to use unique_constraint/3. This is not like Active Record because it is using the database to ensure uniqueness. Active Record would do a query for records with the same value and if any were returned then it would fail. This has a race condition where, if a value is inserted between fetching to check uniqueness and inserting your record, you will either end up with

Ecto association with a condition

白昼怎懂夜的黑 提交于 2019-12-09 08:54:08
问题 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] 回答1: This is not available in Ecto, there is a

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

早过忘川 提交于 2019-12-09 05:12:43
问题 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. 回答1: You can access the test database by using MIX_ENV=test followed by a command

Querying by DateTime in Ecto

泄露秘密 提交于 2019-12-08 17:20:44
问题 Here is what I have tried. date = Ecto.DateTime.from_erl(:calendar.universal_time()) query |> where([record], record.deadline >= ^date) I also tried date = Ecto.DateTime.from_erl(:calendar.universal_time()) query = from m in MyApp.SomeModel, where: m.deadline >= ^date, select: m Both return same message value `%Ecto.DateTime{..}` in `where` cannot be cast to type :datetime in query From what I understand I am supposed to be using Ecto.DateTime in my queries. Maybe I am missing something