Elixir 菲尼克斯外星变量模型法
我正在使用Phoenix web framework(Elixir)重写一个基于python的API交付服务,但遇到了一个小问题 我正在尝试创建一个基本的模型方法,当调用该方法时,您向它传递一个序列号,它通过Ecto查询数据库并返回结果。通过查看文档,我应该能够使用插值来定义查询中的变量,但是我仍然得到一个错误 lib/test\u result.ex文件Elixir 菲尼克斯外星变量模型法,elixir,phoenix-framework,ecto,Elixir,Phoenix Framework,Ecto,我正在使用Phoenix web framework(Elixir)重写一个基于python的API交付服务,但遇到了一个小问题 我正在尝试创建一个基本的模型方法,当调用该方法时,您向它传递一个序列号,它通过Ecto查询数据库并返回结果。通过查看文档,我应该能够使用插值来定义查询中的变量,但是我仍然得到一个错误 lib/test\u result.ex文件 defmodule Webservices.TestResult do use Ecto.Schema schema "t
defmodule Webservices.TestResult do
use Ecto.Schema
schema "test_result" do
field :date_added, Ecto.DateTime
field :serial, :string
field :sequence_id, :integer
field :last_completed_stage, :integer
field :last_completed_sequence, :integer
field :workorder, :string
field :product, :string
field :is_complete, :integer
field :is_scrapped, :integer
field :value_stream, :string
field :promise_date, Ecto.Date
field :fail_lock, :integer
field :sequence_rev, :integer
field :date_updated, Ecto.DateTime
field :date, Ecto.Date
field :time, Ecto.Time
field :ptyp2, :string
field :wo_qty, :integer
field :is_active, :integer
field :is_time_lock, :integer
field :time_lock_timestamp, Ecto.DateTime
field :scrap_reason, :string
field :scrapped_by, :integer
end
end
defmodule Webservices.TestResultDetail do
use Ecto.Schema, :model
import Ecto.Query
schema "test_result_detail" do
field :status_id, :integer
field :station_id, :integer
field :stage_id, :integer
field :operator_id, :integer
field :failstep, :string
field :shift, :integer
field :sequence_rev, :integer
field :date_added, Ecto.Date
field :date_timestamp, Ecto.DateTime
field :date_time, Ecto.Time
field :stage_order, :integer
field :serial_number, :string
field :is_retest, :integer
field :retest_reason, :string
has_many :result_id, Webservices.TestResult
end
def last_completed_test(serial) do
from c in Webservices.TestResultDetail,
join: t in TestResult, on: t.id == c.result_id,
select: {t.serial, c.station_id, c.stage_id, c.operator_id, c.sequence_rev},
where: t.serial == ^serial,
order_by: [desc: c.id],
limit: 1
end
end
lib/test\u result\u detail.ex文件
defmodule Webservices.TestResult do
use Ecto.Schema
schema "test_result" do
field :date_added, Ecto.DateTime
field :serial, :string
field :sequence_id, :integer
field :last_completed_stage, :integer
field :last_completed_sequence, :integer
field :workorder, :string
field :product, :string
field :is_complete, :integer
field :is_scrapped, :integer
field :value_stream, :string
field :promise_date, Ecto.Date
field :fail_lock, :integer
field :sequence_rev, :integer
field :date_updated, Ecto.DateTime
field :date, Ecto.Date
field :time, Ecto.Time
field :ptyp2, :string
field :wo_qty, :integer
field :is_active, :integer
field :is_time_lock, :integer
field :time_lock_timestamp, Ecto.DateTime
field :scrap_reason, :string
field :scrapped_by, :integer
end
end
defmodule Webservices.TestResultDetail do
use Ecto.Schema, :model
import Ecto.Query
schema "test_result_detail" do
field :status_id, :integer
field :station_id, :integer
field :stage_id, :integer
field :operator_id, :integer
field :failstep, :string
field :shift, :integer
field :sequence_rev, :integer
field :date_added, Ecto.Date
field :date_timestamp, Ecto.DateTime
field :date_time, Ecto.Time
field :stage_order, :integer
field :serial_number, :string
field :is_retest, :integer
field :retest_reason, :string
has_many :result_id, Webservices.TestResult
end
def last_completed_test(serial) do
from c in Webservices.TestResultDetail,
join: t in TestResult, on: t.id == c.result_id,
select: {t.serial, c.station_id, c.stage_id, c.operator_id, c.sequence_rev},
where: t.serial == ^serial,
order_by: [desc: c.id],
limit: 1
end
end
我的控制器:
defmodule Webservices.OPTController do
use Webservices.Web, :controller
alias Webservices.Router
import Webservices.Router.Helpers
def last(conn, %{"serial" => serial}) do
import Ecto.Query
results = Webservices.TestResultDetail.last_completed_test(serial)
render(conn, "last.json", results: results)
end
end
我收到以下编译错误:
==文件web/controllers/opt_controller.ex上的编译错误==
**(exto.Query.CompileError)变量serial
不是有效的查询表达式。变量需要在查询中显式插入
与^
扩展宏:exto.Query.where/3
web/controllers/opt_controller.ex:12:Webservices.OPTController.index/2
(elixir)扩展宏:内核。|>/2
web/controllers/opt_controller.ex:13:Webservices.OPTController.index/2
(elixir)lib/kernel/parallel_compiler.ex:117:kernel.parallelciler.spawn_compilers/1中的匿名fn/4
您合并了两个表,在
t
表中有:serial
字段。在c
中有:序列号
。只需在t.serial==^serial
where:c.serial==^serial
->where:c.serial\u number==^serial
而不是t
。我创建了一个answer.c.serial是指向另一个表的FK链接,其中有一个serial字段。测试结果详细信息表中未使用序列号字段,抱歉造成混淆。我在上面更新了我的问题,这是一个明确的胖手指很好的捕捉。我已经更新了,但仍然看到相同的错误。呃,这实际上是我在另一个文件中遇到的另一个打字错误的问题。