ecto

How to select id with max date group by category in Ecto query with Phoenix?

被刻印的时光 ゝ 提交于 2019-12-23 15:01:47
问题 For an example, I would like to select id with max date group by category, the result is: 7, 2, 6 id category date 1 a 2013-01-01 2 b 2013-01-03 3 c 2013-01-02 4 a 2013-01-02 5 b 2013-01-02 6 c 2013-01-03 7 a 2013-01-03 8 b 2013-01-01 9 c 2013-01-01 This is the SQL I think can work: SELECT * FROM Table1 t1 JOIN ( SELECT category, MAX(date) AS MAXDATE FROM Table1 GROUP BY category ) t2 ON T1.category = t2.category AND t1.date = t2.MAXDATE But how to translate that into a query on Ecto? 回答1:

assign @changeset not available in eex template

半腔热情 提交于 2019-12-23 12:57:07
问题 I am trying to learn the Phoenix's Form system with including the Ecto.Model's but i have encountered a problem that i can't pass. I have created a form: <div class="jumbotron"> <%= form_for @changeset, user_path(@conn, :create), fn f -> %> <label> Login: <%= text_input f, :login %> </label> <label> Password: <%= password_input f, :password %> </label> <label> Name: <%= text_input f, :name %> </label> <label> Surname: <%= text_input f, :name %> </label> <label> Email: <%= email_input f, :name

Postgrex - No pg_hba.conf entry for host “xxx.xxx.xxx.xxx”

╄→гoц情女王★ 提交于 2019-12-23 12:27:24
问题 I have a Postgres Database hosted separately which I'm trying to use with my Phoenix Application. My prod config is: config :my_app, MyApp.Repo, adapter: Ecto.Adapters.Postgres, url: "postgres://username:password@myhost:5432/my_database", size: 20 This worked fine while my application was hosted on Heroku, but since I've moved it to a VPS, I keep getting this error: 17:07:13.665 [info] GET /subjects 17:07:13.707 [info] Processing by MyApp.SubjectController.index/2 Parameters: %{"format" =>

How to use Geo library to create valid Ecto Model changeset?

烈酒焚心 提交于 2019-12-22 04:39:20
问题 I'm trying to use Geo library to store Geo.Point via Phoenix model changeset. My params are: {coordinates: [49.44, 17.87]} or more prefer would be {coordinates: {latitude: 49.44, longitude: 17.87}} In iex console I tried: iex(5)> changeset = Place.changeset(%Place{}, %{coordinates: [49.44, 17.87]}) %Ecto.Changeset{action: nil, changes: %{}, constraints: [], errors: [coordinates: "is invalid"], filters: %{} model: %Myapp.Place{__meta__: #Ecto.Schema.Metadata<:built>, coordinates: nil, id: nil,

Many-to-Many with ECTO and put_assoc/4

与世无争的帅哥 提交于 2019-12-21 21:42:18
问题 I try associate 2 existing Many-to-Many records with ECTO and put_assoc/4 but won't remove elements when try update. Basically i have projects and users . for manage the access of users to projects i have the table "user_project". def Project do schema "project" do ... # users (if user_type is :admin) many_to_many( :users, User, join_through: "user_project" ) ... end end def User do schema "user" do ... # users (if user_type is :admin) many_to_many( :projects, User, join_through: "user

Phoenix framework - Custom changeset validations

跟風遠走 提交于 2019-12-21 07:38:08
问题 I'm really new to phoenix and elixir, so my apologies if these seem like simple questions. I've searched stack overflow and blogs before I thought about posting it here. I've got 2 fields in a model, field A : integer and field B : integer. When doing my validations with my changeset I want to create a custom validation that checks if field A is more than field b when creating a new item, and if so then flash a error message and bring them back to the :new route. Sorry if I'm not using the

Ecto remove preload

随声附和 提交于 2019-12-19 09:37:36
问题 Is there any way to do the inverse to preload? %Post{ comments: [] } posts = Repo.all(Post) |> Repo.unload(:comments) %Post{ comments: #Ecto.Association.NotLoaded<association :comments is not loaded>, } 回答1: Ecto.Association.NotLoaded is a plain old simple struct, so you might relatively easy implement this unpreload youself: defmodule Unpreloader do def forget(struct, field, cardinality \\ :one) do %{struct | field => %Ecto.Association.NotLoaded{ __field__: field, __owner__: struct.__struct_

Should I use Ecto.Repo in Controller or Model for Elixir Phoenix?

纵饮孤独 提交于 2019-12-18 20:49:21
问题 For some query in Controller of Phoenix, there're two plans for me Plan 1: defmodule Demo.UserController do # ... def index do # This is just for example # The point is Repo in used here Repo.all(User) end end Plan 2: defmodule Demo.User do # ... def all do # Put all Repo API and building query logic in Model Repo.all(__MODULE__) end end I prefer the Plan 2. Because in most situations, I can put all logic about fetching data in Model. But I find official guide use Plan 1(docs/model) and

Can I get Ecto to log raw SQL?

只愿长相守 提交于 2019-12-18 09:16:49
问题 I am building an Ecto query like this: from item in query, where: like(item.description, ^"%#{text}%") I'm concerned that this allows SQL injection in text . Before trying to fix that, I want to see how the query is actually sent to the database. If I inspect the query or look at what is logged, I see some SQL, but it's not valid. For instance, inspecting the query shows me this: {"SELECT i0.\"id\", i0.\"store_id\", i0.\"title\", i0.\"description\" FROM \"items\" AS i0 WHERE (i0.\"description

Turn postgres date representation into ISO 8601 string

吃可爱长大的小学妹 提交于 2019-12-17 23:27:25
问题 I'm trying to format a Postgres date representation into a ISO 8601 string. I'm assuming that there is a Postgres function that can do it, but I found the documentation short on examples. My query is SELECT now()::timestamp which returns [{{2016, 8, 9}, {3, 56, 55, 754181}}] I'm trying to get the date into a format that looks more like 2016-8-9T03:56:55+00:00 . What changes do I need to make to my query to make that happen? Thanks for your help. 回答1: I think I found a way to do the formatting