ecto

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

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

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

How to implement to_query(data) in Elixir Struct

泪湿孤枕 提交于 2019-12-12 03:38:50
问题 I am attempting to update a existing records in my database using Repo.update: def subscribe_email(conn, %{"email-address"=>email_address, "shop"=>shop}) do current_record = Repo.all(%Oauth.EmailAddress{email_address: email_address, active: false, shop: shop, verified: :true}) current_record = Ecto.Changeset.change(current_record, active: :true) case Repo.update current_record do {:ok, struct} -> IO.puts "updated correctly." {:error, changeset} -> IO.puts "did not update" end end I have a

How to query a third relation in Ecto Associations

♀尐吖头ヾ 提交于 2019-12-11 17:59:23
问题 Snapmail |> preload(:user) |> preload(:snapmail_cameras) |> preload([snapmail_cameras: :camera]) |> preload([snapmail_cameras: [camera: :vendor_model]]) |> preload([snapmail_cameras: [camera: [vendor_model: :vendor]]]) |> Repo.all I have above query in Ecto. in this. Each snapmail has a snapmail_camera and snapmail_camera has a camera_id which is camera's id (from camera table.) and Cameras table has a field status . I am trying to get only those snapmails where its snapmail_cameras 's camera

Migration database constraint

杀马特。学长 韩版系。学妹 提交于 2019-12-11 14:19:14
问题 I'm writing a migration file and one of the fields I'd like to set a DB (Postgres) constraint for: def change do create table(:reviews) do add(:title, :string) add(:contents, :string) add(:stars, :integer) add(:user_id, references(:users), null: false) add(:user_id, references(:users), null: false) timestamps() end Id like to set a constraint on the stars field to be integer values only 1-5. How can I do this with Ecto.Migration? 回答1: How can I do this with Ecto.Migration Ecto.Migration

Can the Ecto schema field name different from column name?

时光毁灭记忆、已成空白 提交于 2019-12-11 12:55:35
问题 I've got a legacy database that I'm trying to pull into Ecto. In it there's an orders table which has an order_status_id column. order_status_id maps to a set of constants in the legacy system. I'd like to have the MyApp.Order struct contain an order_status field, which has a custom type that converts the integer IDs to meaningful atoms. I've got the custom type working, but I can't figure out how to map a field named order_status to a column named order_status_id . The legacy system is still

Insert null into ecto belongs_to field

末鹿安然 提交于 2019-12-11 09:45:39
问题 I have an ecto model defmodule App.Profile do use App.Web, :model alias App.Repo schema "profiles" do belongs_to :user, App.User field :name, :string field :last_name, :string field :second_surname, :string timestamps end But sometime I want to save to database and put the user to nil, Need I to add some flag to the user field? I have this code in my controller changeset = Profile.changeset(%Profile{user_id: nil}, profile_params) But when try to save I got this error :erlang.byte_size({:user

How to write a Ecto query that does a group_by MONTH on a datetime field

落爺英雄遲暮 提交于 2019-12-11 07:39:18
问题 I am doing a ecto query and am trying to group by q.created_date . This query successfully does the GROUP BY but it does it by the second. I am trying to group by month instead. MYQUERY |> group_by([q], [q.created_date, q.id]) Is there something like: MYQUERY |> group_by([q], [month(q.created_date), q.id]) 回答1: You can use fragment with date_part('month', field) to extract the month in PostgreSQL: fragment("date_part('month', ?)", p.inserted_at)) iex(1)> from(Post) |> select([p], p.inserted