elixir

How to add timestamps to an existing table with Ecto's timestamps?

元气小坏坏 提交于 2019-12-12 10:36:46
问题 Since inserted_at and updated_at can not be null this won't work: def change do alter table(:channels) do timestamps end end ** (Postgrex.Error) ERROR (not_null_violation): column "inserted_at" contains null values Is there an easy way to accomplish this without copying timestamps ' functionality? 回答1: The timestamps/1 function accepts an options keyword list, you can set the default value with it. def change do alter table(:channels) do timestamps default: "2016-01-01 00:00:01", null: false

Elixir - Multiple expressions on same line - Compiler error when using do: syntax in function definition

泪湿孤枕 提交于 2019-12-12 10:36:32
问题 In Elixir, multiple expressions can be delimited by semicolon ( ; ). Elixir complains in below function definition defmodule Module2 do def func([c], n), do: IO.inspect(c); c + n end with error ** (CompileError) hello.exs:2: undefined function c/0 (stdlib) lists.erl:1352: :lists.mapfoldl/3 (stdlib) lists.erl:1352: :lists.mapfoldl/3 (stdlib) lists.erl:1353: :lists.mapfoldl/3 However, Elixir is happy with below syntax. defmodule Module1 do def func([c], n) do IO.inspect(c); c + n end end I am

Elixir/Phoenix: How to use third-party modules in config files?

江枫思渺然 提交于 2019-12-12 10:33:26
问题 It seems that the way configuration files in phoenix are loaded and compiled pose a problem when using third-party modules in config.exs or dev.exs/prod.exs/test.exs . Example: To set up Guardian for JWT authentication I am trying to use the JOSE.JWK module for JWK creation / loading in my config.exs . I can use the module alright in the console with iex -S mix phoenix.server . It is of course installed as a dependency. The error I'm getting is ** (Mix.Config.LoadError) could not load config

Phoenix: Ordering a query set

孤人 提交于 2019-12-12 09:42:29
问题 I'm [a noob] playing around with the Phoenix framework for fun and building a small twitter clone. I everything working, however, I want to order the tweets by the updated_at field (ascending). As you can see from tweet_controller, I have tried messing around with an order_by clause, but this did nothing for me. Question How do I achieve this? Within the EEx or within the tweet_controller itself? tweet/index.html.eex <div class="row"> <%= for tweet <- @tweets do %> <h4><%= tweet.tweet %></h4>

How to make forms and transactions play well in phoenix + ecto?

怎甘沉沦 提交于 2019-12-12 09:11:35
问题 I'm playing with Phoenix + Ecto and I stumbled upon something that does not feel idiomatic to me. I have a form that represents an Invitation . When creating an Invitation we also need to create a User and obviously I want both to happen in a transaction so I keep data consistency. In my form I ask for name and email . Since I want the Invitation changeset in my view to represent the errors correctly I ended up with this code... but does not look great. Do you know a better way to do this in

Elixir processes and no shared heap memory

三世轮回 提交于 2019-12-12 09:07:38
问题 Elixir processes have their own heap. If a process wants to share a data structure with another process, how could that be possible? One answer that comes to my mind is that the process sends a message to the other process containing the data structure. Does that mean that the entire data structure is copied from one heap to the other? And if this is true, isn't it inefficient? 回答1: TL;DR: Yes, it is inefficient. But you'll almost never notice this in practice . Welcome to the world of

(Protocol.UndefinedError) protocol Enumerable not implemented for 3

那年仲夏 提交于 2019-12-12 05:19:08
问题 I'm trying to return a summed amount after a comprehension. Here is what I'm trying: range = 1..999 multiple_of_3_or_5? = fn(n) -> (rem(n, 3) == 0 || rem(n, 5) == 0) end for n <- range, multiple_of_3_or_5?.(n), do: Enum.reduce(n, 0, fn(x, y) -> (x + y) end) This seems like it should sum the list that is returned from the comprehension but instead it prints this error: #=> ** (Protocol.UndefinedError) protocol Enumerable not implemented for 3 Can anyone help with this? 回答1: You're passing each

HTTP 500 Deploying Elixir/Phoenix to AWS Elastic Beanstalk

浪子不回头ぞ 提交于 2019-12-12 05:05:08
问题 I'm having trouble with the elixir/phoenix config that I need for a deployment to AWS/Elastic Beanstalk. (Following the guide found here: https://thoughtbot.com/blog/deploying-elixir-to-aws-elastic-beanstalk-with-docker - my Dockerfile looks similar except for updated libraries). I can run in eb local run , but am having trouble pushing to production. However, when I try and deploy to EB, I get the following warning, and it crashes: Environment health has transitioned from Degraded to Severe.

mix ecto.create connection refused

杀马特。学长 韩版系。学妹 提交于 2019-12-12 04:57:19
问题 I received this error when trying to run mix ecto.create : 13:27:47.442 [error] GenServer #PID<0.3189.0> terminating ** (DBConnection.ConnectionError) tcp connect (localhost:5432):connection refused - :econnrefused (db_connection) lib/db_connection/connection.ex:148:DBConnection.Connection.connect/2 (connection) lib/connection.ex:622: Connection.enter_connect/5 (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3 Last message: nil State: Postgrex.Protocol ** (Mix) The database for Hello

Can't understand destructuring in JWT auth (Phoenix)

隐身守侯 提交于 2019-12-12 04:54:28
问题 I'm setting up a pattern that I've seen a few places for API authentication in Phoenix, using Comeonin and Guardian for JWT auth. When I POST to MyApp.SessionsController.create/2 from CURL, I get a user response back from MyApp.Session.authenticate/1 as I would expect. However, I'm supposed to destructure it into {:ok, jwt, _full_claims} , which can then be piped to Guardian. I use IO.inspect user to look at the user object and get the following error: Terminal: curl -H "Content-Type: