根据这个问题中有关如何使用当前项目的依赖关系加载iex 的建议,我能够以非常高效的方式使用phoenix框架依赖项.但是,为每一件事提供Phoenix项目的命名空间都会有点乏味.
而不是打字,MyApp.Repo.all(MyApp.User)我希望能够做到Repo.all(User).我可以单独为每个东西添加别名,alias MyApp.Repo, as: Repo但有没有办法一次性完成所有这些操作?
我正在尝试编写一个更新模型并插入关联的变更集.我找不到如何使用的例子put_assoc/4
order = order
|> Proj.Order.changeset(%{state: "error", error_count: order.error_count + 1})
|> Ecto.Changeset.put_assoc(
:order_errors,
[Proj.OrderError.changeset(%Proj.OrderError{}, %{reason: "not_found"})])
|> Proj.Repo.update!
Run Code Online (Sandbox Code Playgroud)
这会打印出以下错误:
** (Ecto.InvalidChangesetError) could not perform update because changeset is invalid.
* Changeset changes
%{order_errors: [%Ecto.Changeset{action: :insert, changes: %{id: nil, inserted_at: nil, order_id: nil, reason: "not_found", updated_at: nil}, constraints: [], errors: [order_id: "can't be blank"], filters: %{}, model: %Proj.OrderError{__meta__: #Ecto.Schema.Metadata<:built>, id: nil, inserted_at: nil, order: #Ecto.Association.NotLoaded<association :order is not loaded>, order_id: nil, reason: nil, updated_at: nil}, optional: [], opts: [], …Run Code Online (Sandbox Code Playgroud) 既然inserted_at又updated_at不可能null这不起作用:
def change do
alter table(:channels) do
timestamps
end
end
Run Code Online (Sandbox Code Playgroud)
** (Postgrex.Error) ERROR (not_null_violation): column "inserted_at" contains null values
有没有一种简单的方法来实现这一点而不复制timestamps'功能?
我还在尝试如何处理has_many, through:在Ecto中创建/更新关联的问题.我重新阅读了José关于协会和文档的帖子,但我仍在努力.
我有的是这个:
网络/模型/ dish.ex
defmodule Mp.Dish do
use Mp.Web, :model
schema "dishes" do
# ...
has_many :dish_dietary_prefs, Mp.DishDietaryPref, on_delete: :delete_all,
on_replace: :delete
has_many :dietary_prefs, through: [:dish_dietary_prefs, :dietary_pref]
end
# ...
end
Run Code Online (Sandbox Code Playgroud)
网络/模型/ dietary_pref.ex
defmodule Mp.DietaryPref do
use Mp.Web, :model
schema "dietary_prefs" do
# ...
has_many :dish_dietary_prefs, Mp.DishDietaryPref, on_delete: :delete_all,
on_replace: :delete
has_many :dishes, through: [:dish_dietary_prefs, :dish]
end
# ...
end
Run Code Online (Sandbox Code Playgroud)
网络/模型/ dish_dietary_pref.ex
defmodule Mp.DishDietaryPref do
use Ecto.Schema
schema …Run Code Online (Sandbox Code Playgroud) 我最近将我的凤凰项目升级到了Ecto 2.0.2.我有一些代码用于Task.Supervisor.async_nolink在自己的线程上对db进行一些更新.我的测试运行时出现以下错误(仅在我的测试中发生)
[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
Run Code Online (Sandbox Code Playgroud)
现在我想我了解发生了什么:在db事务完成之前,正在检查Ecto Sandbox连接池.根据文档(至少我读它们的方式),解决这些问题的方法是使用共享连接池:Ecto.Adapters.SQL.Sandbox.mode(MyApp.Repo, {:shared, self()})我正在做的事情.不幸的是,这不起作用.
如何设置我的测试以便不会发生此错误?
有人可以提供一个简单的例子defdelegate.我找不到任何东西,让人难以理解.
defmodule Dummy do
def hello, do: "hello from dummy"
end
Run Code Online (Sandbox Code Playgroud)
我得到undefined function world/0以下内容:
defmodule Other do
defdelegate hello, to: Dummy, as: world
end
Run Code Online (Sandbox Code Playgroud)
我想委托Other.world给Dummy.hello
我有一个针对多个数据库的应用程序,它就像一个应用程序,以一种无法静态控制的方式(在配置文件中)监视制作报告,调解通知,运行任务和内容的应用程序,所以我认为它可以做一个主管,监督多个Repos只是改变它的设置opts(我想在一个回购池,这样的事情,听起来很疯狂,我知道),但它是不可能的,因为Ecto.Repo有一个宏使连接绑定到Repo的模块,所以,有一种方法Ecto.Repo可以用我能满足需求的方式重写它吗?我查看了ecto的消息来源,但我发现没有任何解决方案可以单独挖掘.
我也查看了这篇文章,但是应用程序可能会针对多个实例,具有不同的用户,不同的凭据等.
我已经达到了一些使用Code.eval创建Repos的解决方案,但我不知道它是多么"好",如果鼓励它.
有没有办法在doctests中使用模块别名?我不想每次都输入一个长名字.
defmodule SomeLongModuleName.SubModule do
alias SomeLongModuleName.SubModule, as: SubModule
@doc """
iex> SubModule.method(%{property_a: 1, property_b: 2) # CompileError
3
"""
def method(%{property_a: a, property_b: b) do
a + b
end
end
Run Code Online (Sandbox Code Playgroud)
上面的示例显示了我可能希望使用别名来避免长行的情况.是否可以在doctest中使用别名?
正如标题所述,我的问题是:
有没有办法确定Elixir中名称存在的模块?
在环顾四周后,我在Elixir论坛中遇到了这个帖子,但并不完全是我正在寻找的.在这个帖子中他们提到Code.ensure_loaded/1,但我认为这不是我需要的.
现在我正在通过以下方式解决问题:
def module_exists?(module_name) where is_atom(module_name) do
!is_nil(module_name.module_info)
rescue
e in UndefinedFunctionError -> false
end
Run Code Online (Sandbox Code Playgroud)
但我不相信.
任何帮助表示赞赏,谢谢!
在Elixir中,有没有办法直接从shell调用模块函数,而不需要启动iex -S mix会话?让我用一个场景来说明:
作为我的Phoenix应用程序的一部分,我编写了一个辅助模块,可以从相邻的iex -S mix会话中运行.这是一个超级简化的版本:
defmodule MyApp.Helper do
# For the demo, these imports/aliases are not used - but they're there in real life.
import Ecto.Query
alias MyApp.Repo
def start do
{:ok, "Done"}
end
end
Run Code Online (Sandbox Code Playgroud)
如果我启动会话iex -S mix然后从模块运行一个函数,它一切正常:
$ iex -S mix
Erlang/OTP 20 [erts-9.2] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
Compiling 2 files (.ex)
Interactive Elixir (1.5.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> MyApp.Helper.start
{:ok, …Run Code Online (Sandbox Code Playgroud)