ecto

Why are pin operators necessary in Ecto queries?

房东的猫 提交于 2019-11-29 01:50:36
问题 In Elixir, the pin operator is used to prevent variable rebinding. However, with regard to an Ecto query like from u in User, where: u.username == ^username the authors of Programming Phoenix state (in chapter 7) that Remember, the ^ operator (called the pin operator) means we want to keep ^username the same. But this doesn't sound right, because apparently, the comparison in the query shall not cause any rebinding of variables. Are the authors of the book (which José Valim co-authored)

Turn postgres date representation into ISO 8601 string

会有一股神秘感。 提交于 2019-11-28 21:12:19
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. I think I found a way to do the formatting, but it's not ideal because I'm writing the formatting myself. Here is a potential solution: SELECT to_char

Encoding a Ecto Model to JSON in elixir

元气小坏坏 提交于 2019-11-28 18:36:55
I am going over the following tutorial in an attempt to get my head around elixir and phoenix: https://thoughtbot.com/blog/testing-a-phoenix-elixir-json-api I am running into an issue with the test, mainly using Poison.encode! on the Contact model. I get the following error: unable to encode value: {nil, "contacts"} This led me to the following issue: https://github.com/elixir-lang/ecto/issues/840 and the fix: https://coderwall.com/p/fhsehq/fix-encoding-issue-with-ecto-and-poison I have added the code from the blog article into lib/poison_encoder.ex, but I now get the following error: no

How to use raw sql with ecto Repo

核能气质少年 提交于 2019-11-28 18:09:59
I have an upsert requirement, so I need to call a postgres stored procedure or use a common table expression. I also use the pgcrypto exgtension for passwords and would like to use postgres functions (such as "crypt" to encode/decode passwords). But I can not find a way to get Ecto to play with raw sql in part or whole, is it intended that ecto will only support the elixir dsl and not allow shelling out to raw sql when the dsl is not sufficient? I've found that I can query via the adapter (Rocket is the name of the app) q = Ecto.Adapters.Postgres.query(Rocket.Repo,"select * from users limit 1"

Ecto 2.0 SQL Sandbox Error on tests

女生的网名这么多〃 提交于 2019-11-28 00:26:34
问题 I recently upgraded my phoenix project to Ecto 2.0.2. I have some code that is using Task.Supervisor.async_nolink to make some updates to the db on its own thread. I am getting the following error when my tests run (only occurs on my tests) [error] Postgrex.Protocol (#PID<0.XXX.0>) disconnected: ** (DBConnection.ConnectionError) owner #PID<0.XXX.0> exited while client #PID<0.XXX.0> is still running with: shutdown Now I think I understand whats happening: The Ecto Sandbox connection pool is

Change Url to accept a string instead id in phoenix framework (elixir)

泪湿孤枕 提交于 2019-11-27 23:53:59
问题 I am trying to implement permalinks in phoenix app. The goal is to change localhost:4000/products/1 to localhost:4000/products/productname I tried following Ryan Bates episode on permalinks implementation in rails but wasn't able to find a to_param function for models in phoenix. Please help. 回答1: Not sure if this is what you are asking for but here you go: router.ex in the browser stack get "/products/:product_name", ProductController, :get_product_by_name product_controller.ex def get

Render many to many relationship JSON in Phoenix Framework

﹥>﹥吖頭↗ 提交于 2019-11-27 21:38:32
I have the next models defmodule App.User do use App.Web, :model alias App.User schema "users" do field :name, :string has_many :roles_users, App.RolesUser has_many :roles, through: [:roles_users, :role] timestamps end end defmodule App.Role do use App.Web, :model schema "roles" do has_many :roles_users, App.RolesUser has_many :users, through: [:roles_users, :user] field :name, :string timestamps end end defmodule App.RolesUser do use App.Web, :model schema "roles_users" do belongs_to :role, App.Role belongs_to :user, App.User timestamps end end Is for a many to many relationship. My

Encoding a Ecto Model to JSON in elixir

烂漫一生 提交于 2019-11-27 11:31:59
问题 I am going over the following tutorial in an attempt to get my head around elixir and phoenix: https://thoughtbot.com/blog/testing-a-phoenix-elixir-json-api I am running into an issue with the test, mainly using Poison.encode! on the Contact model. I get the following error: unable to encode value: {nil, "contacts"} This led me to the following issue: https://github.com/elixir-lang/ecto/issues/840 and the fix: https://coderwall.com/p/fhsehq/fix-encoding-issue-with-ecto-and-poison I have added

Render many to many relationship JSON in Phoenix Framework

时间秒杀一切 提交于 2019-11-27 04:32:09
问题 I have the next models defmodule App.User do use App.Web, :model alias App.User schema "users" do field :name, :string has_many :roles_users, App.RolesUser has_many :roles, through: [:roles_users, :role] timestamps end end defmodule App.Role do use App.Web, :model schema "roles" do has_many :roles_users, App.RolesUser has_many :users, through: [:roles_users, :user] field :name, :string timestamps end end defmodule App.RolesUser do use App.Web, :model schema "roles_users" do belongs_to :role,