ecto

Modify foreign key in Ecto

 ̄綄美尐妖づ 提交于 2019-12-02 20:15:38
I have this original migration that have already been run and sent upstream: create table(:videos) do add :url, :string add :title, :string add :description, :text add :user_id, references(:users, on_delete: :nothing) timestamps end create index(:videos, [:user_id]) Now i wish to change the foreign key on user_id to cascade deletions, so that when a user is deleted all of his associated videos will also be deleted. I have tried the following migration: alter table(:videos) do modify :user_id, references(:users, on_delete: :delete_all) end But this raises an error: (Postgrex.Error) ERROR

How to handle associations and nested forms in Phoenix framework?

痴心易碎 提交于 2019-12-02 19:07:39
What is the way to handle associations and nested forms in Phoenix framework? How would one create a form with nested attributes? How would one handle it in the controller and model? There is a simple example of handling 1-1 situation. Imagine we have a Car and an Engine models and obviously a Car has_one Engine . So there's code for the car model defmodule MyApp.Car do use MyApp.Web, :model schema "cars" do field :name, :string has_one :engine, MyApp.Engine timestamps end def changeset(model, params \\ :empty) do model |> cast(params, ~w(name), ~w()) |> validate_length(:name, min: 5, message:

Is there a Phoenix equivalent to Rails Console

馋奶兔 提交于 2019-12-02 16:54:39
I'm just learning Phoenix and Elixir and I'm coming from Ruby/Rails where I work in the REPL using pry to inspect my database and application state. I'm trying to figure out how to interact with my database and models in a Phoenix app. I'm aware of iex , but I don't know how to use it inspect my app's database from the repl. Do I need to connect to it with ecto each time from the repl? Is there a rails console equivalent. I've checked the Phoenix docs, Elixir Dose, and the Ecto repo, but can't find what I'm looking for. Am I missing something? Edit: Based on the answer below I found this

Ecto remove preload

自作多情 提交于 2019-12-02 02:06:45
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>, } 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__, __cardinality__: cardinality } } end end And use it later as: Unpreloader.forget(%Post{....}, :comments)

How to configure Ecto at runtime?

只谈情不闲聊 提交于 2019-12-01 22:21:04
问题 Following the setup instructions, I have the following Ecto configuration in my config/config.exs file : config :my_app, MyApp.Repo, adapter: Ecto.Adapters.Postgres, url: "postgresql://postgres@localhost/myrepo", size: 20 If my understanding is correct, the config.exs is evaluated at compile-time. Is there a way to do this configuration step at runtime ? This is for an app which will be distributed as a compiled binary (via exrm ). The end-user should be able to customize the database url and

Generic composable Ecto query w/ dynamic field name in query expression

ぐ巨炮叔叔 提交于 2019-12-01 16:43:59
I'm trying to allow for passing in a field name and running it in an Ecto query expression dynamically, like so: def count_distinct(query, field_name) when is_binary(field_name) do query |> select([x], count(Map.fetch!(x, field_name), :distinct)) end However, I get this compilation error: (Ecto.Query.CompileError) `Map.fetch!(x, field_name)` is not a valid query expression Is there any way to accomplish this? You need to use field/2 to dynamically generate fields in queries: query |> select([x], count(field(x, ^field_name), :distinct)) An example using the other query syntax for completion:

Generic composable Ecto query w/ dynamic field name in query expression

纵饮孤独 提交于 2019-12-01 15:24:58
问题 I'm trying to allow for passing in a field name and running it in an Ecto query expression dynamically, like so: def count_distinct(query, field_name) when is_binary(field_name) do query |> select([x], count(Map.fetch!(x, field_name), :distinct)) end However, I get this compilation error: (Ecto.Query.CompileError) `Map.fetch!(x, field_name)` is not a valid query expression Is there any way to accomplish this? 回答1: You need to use field/2 to dynamically generate fields in queries: query |>

Ecto: Order preloaded data in collection with has_many association

帅比萌擦擦* 提交于 2019-11-30 20:10:29
Let's say I have this for fetching all threads: Thread |> Thread.ordered |> Repo.all |> Repo.preload([:posts]) How could I provide an order_by clause for :posts ? I can't seem to locate anything in the documentation that references Ecto.Repo.preload/1 , so none of the provided examples appear helpful for figuring out how to use this syntax properly. The Ecto.Query module makes it really easy to also apply certain queries to things like preloading. The way we achieve this is by passing a query into the preload function, which then restricts the results of the preload to that query. For example,

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

帅比萌擦擦* 提交于 2019-11-30 19:01:11
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 Phoenix default code alias Repo in Controller instead of Model ( web/web.ex ) Which one is better? And

How to run updating in migration for Ecto?

荒凉一梦 提交于 2019-11-30 13:39:32
I use Phoenix and Ecto in one of my project. I want to add a column to one table, and I expect it to be a NOT NULL column. But I already have some existed data, so I decide to add the column, update all rows to some value and modify the column to NOT NULL . I tried these two code: # solution 1 def up do alter table(:channels) do add :type, :integer Exchat.Repo.update_all("channels", set: [type: 1]) modify :type, :integer, null: false end end # solution 2 def up do alter table(:channels) do add :type, :integer end Exchat.Repo.update_all("channels", set: [type: 1]) alter table(:channels) do