ecto

Why does Phoenix (ecto/Postgresx) fail to Connect in dev

自作多情 提交于 2019-12-08 16:38:22
问题 I am beginning my Elixir/Phoenix journey and having some trouble with my postgres connection. When I start up my server I get: $ mix phoenix.server [error] Postgrex.Protocol (#PID<0.214.0>) failed to connect: ** (Postgrex.Error) tcp connect: connection refused - :econnrefused [error] Postgrex.Protocol (#PID<0.217.0>) failed to connect: ** (Postgrex.Error) tcp connect: connection refused - :econnrefused [error] Postgrex.Protocol (#PID<0.218.0>) failed to connect: ** (Postgrex.Error) tcp

Phoenix - return Ecto query results to a specific client

醉酒当歌 提交于 2019-12-08 02:43:46
问题 I'm currently trying to devise a scheme where the following happens. Client A is subscribed/connected to topic/channel T . A sends a message in the form of a select query to T . Only A receives the query results, no other subscribers. Is this even possible using Channels? The main reason I chose Channels was for the excellent websocket support - however I'm open to other Phoenix solutions. 回答1: Yes, Channels should do the work you want. You can push the query results down to the user who sent

Ecto: How to preload records with selecting another joined columns

不想你离开。 提交于 2019-12-07 12:23:10
问题 Are there any ways to preload records by selecting another joined columns? # table structure # User 1---* Post 1---* PostTag *---1 Tag # extract definition of scheme scheme "posts" do ... has_many :post_tags, PostTag has_many :tags, [:post_tags, :tag] end Following pseudo-code expresses my goal(but not work). query = from post in Post, join: user in User, on post.user_id == user.id, select: %{ id: post.id, title: post.title, user_name: user.name, # <= column at joined table }, preload: [:tags

Adding timestamps to an existing table with Ecto's timestamps

雨燕双飞 提交于 2019-12-07 01:21:23
问题 This has been asked here How to add timestamps to an existing table with Ecto's timestamps?, however the accepted solution means that every new entry will have the same default time. I would want the new entries to have the correct time of insert/update. eg. # set default to given date to fill in all existing rows with timestamps def change do alter table(:my_table) do timestamps(default: "2018-01-01 00:00:01") end end If this is all that is in the migration, every inserted_at and updated_at

Setting up custom response for exception in Phoenix Application

泪湿孤枕 提交于 2019-12-06 18:47:36
问题 im writing phoenix application with ecto and have the following snippet in the test {:ok, data} = Poison.encode(%{email: "nonexisting@user.com", password: "mypass"}) conn() |> put_req_header("content-type", "application/json") |> put_req_header("accept", "application/json") |> post(session_path(@endpoint, :create), data) > json_response(:not_found) == %{} this throws a Ecto.NoResultsError i have this defined defimpl Plug.Exception, for: Ecto.NoResultsError do def status(_exception), do: 404

Count the Number of Entries in an Ecto Repository

拥有回忆 提交于 2019-12-06 16:38:02
问题 Whats the quickest way to see the number of entries in my database? I'd like to see the number of posts in my posts/index view. Say I have a Post model and a bunch of posts saved in my database. In Rails I could do something like this in a view file: <h1><%= @posts.length %> Posts</h1> or <h1><%= @posts.size %> Posts</h1> or <h1><%= @posts.count %> Posts</h1> What's the Phoenix Framework/Elixir equivalent? 回答1: If you've already loaded the posts in memory in your controller using Repo.all ,

Phoenix - return Ecto query results to a specific client

百般思念 提交于 2019-12-06 08:43:43
I'm currently trying to devise a scheme where the following happens. Client A is subscribed/connected to topic/channel T . A sends a message in the form of a select query to T . Only A receives the query results, no other subscribers. Is this even possible using Channels? The main reason I chose Channels was for the excellent websocket support - however I'm open to other Phoenix solutions. Yes, Channels should do the work you want. You can push the query results down to the user who sent the query using push : def handle_in("new_query", %{"query" => query}, socket) do # do the query and store

How to convert a String into an Ecto.DateTime in Elixir?

為{幸葍}努か 提交于 2019-12-06 06:15:49
问题 I need to convert a string containing a valid UTC time to an Ecto.DateTime one, which I will insert it into my database with the correct format later. I have tried using the Ecto.DateTime.cast(date) method but it doesn't seem to work. The string is Sat Aug 04 11:48:27 +0000 2012 and comes from the Twitter API. I know there are libraries such as Timex which I didn't inspect yet. Is there any easy working solution already built in Elixir? 回答1: There's no built-in solution in Elixir or Erlang

Inverse polymorphic with ecto

青春壹個敷衍的年華 提交于 2019-12-06 05:25:09
问题 the current Ecto documentation http://hexdocs.pm/ecto/Ecto.Schema.html only explains how to build a belongs_to type of polymorphic association, when the polymorphic Comment can belong to both Task and Post . But what about opposite direction? For example there is a Listing which can have a one of the four types of properties: Room , Apartment , Vila or Office . Considering a one-to-one relationship, given the example above it would mean that there should be rooms_listings , apartments

How to type cast decode JSON as if it came from the database

六眼飞鱼酱① 提交于 2019-12-06 03:35:48
问题 When loading date/time types from the database, Ecto will cast to a Ecto.DateTime type. How can the same type casting be applied when loading a model from a JSON string defmodule Rocket.User do use Rocket.Model schema "users" do field :created_at, :datetime field :name, :string field :email, :string field :password, :string field :timezone, :string end end iex(40)> Poison.decode!(~s({"created_at":"2015-01-21T06:05:10.891Z"}), as: Rocket.User) %Rocket.User{created_at: "2015-01-21T06:05:10.891Z