无法在Exto/Elixir中正确连接
我有一个ParentModel,ChildModel1和ChildModel2。ParentModel有许多ChildModel1,ChildModel1有许多ChildModel2无法在Exto/Elixir中正确连接,elixir,phoenix-framework,ecto,Elixir,Phoenix Framework,Ecto,我有一个ParentModel,ChildModel1和ChildModel2。ParentModel有许多ChildModel1,ChildModel1有许多ChildModel2 data = ChildModel2 |> join(:left, [chmd2], chmd1 in assoc(chmd2, :child_model1)) |> where(ChildItem2, [a], a.child_model1.parent_mod
data = ChildModel2
|> join(:left, [chmd2], chmd1 in assoc(chmd2, :child_model1))
|> where(ChildItem2, [a], a.child_model1.parent_model_id == ^get_some_value()) # get_some_value() returns a number
|> Repo.all()
这里有一个例外:
cannot use ^get_some_value() outside of match clauses
当我用文字替换它时
|> where(ChildItem2, [a], a.child_model1.parent_model_id == 123)
我得到:
undefined function where/4
更新
错误仍然存在:
data = ChildModel2
|> join(:left, [chmd2], chmd1 in assoc(chmd2, :child_model1))
|> where([a], a.child_model1.parent_model_id == 123) # [a] is ChildModel2
|> Repo.all()
错误:
a.child_model1().parent_model_id() is not a valid query expression
您不应该在
的where
中插入ChildItem2
,因为它已经作为join
的结果作为第一个参数传递
该问题是由
^
(pin运算符)引起的,当它连接到函数调用时,它永远不会工作-即使在情况下
或条件下
。尝试简单地将get_some_value()
的结果赋给变量。这个问题是由^(pin运算符)引起的,当它附加到函数调用时它永远不会工作-即使在case或cond中它也会工作,到目前为止,我在一个稍微不同的查询中遇到了它,并且工作得很好。我已经替换了get_some_value()
带有文本。错误仍然存在:a.child\u model1().parent\u model\u id()
不是有效的查询表达式。是否确实正确添加了此关联<代码>子模型1和父模型id是字段,而不是函数。您是否在架构和迁移中添加了child\u model1
字段?还请记住,assoc
不保证关联已加载。是否从where
中删除此childItem
?是否确实允许在“where”中按父模型进行查询?此模块中是否有import-exto.query
?如果宏未导入到该模块中,您将得到“无法使用^outside…”错误。对于第二个错误,我想您需要|>where([chmd2,chmd1],chmd1.parent\u model\u id==123)
@Dogbert,使用pin运算符无法处理函数调用。@PatNowakexto.Query。其中是一个宏。(你是说别的吗?)是的,我给你写了一条评论,说即使加载了宏,pin操作符也不能在那里使用函数调用get\u some\u value()
。现在这个问题被编辑了,所以它不再困扰@Torito了,但主要问题仍然存在。