在下面的链接做了一些研究
https://github.com/elixir-lang/ecto/tree/master/examples/simple
关于如何在灵药中使用ecto,我有点困惑.
有总是声明如下架构
defmodule Weather do
use Ecto.Model
schema "weather" do
field :city, :string
field :temp_lo, :integer
field :temp_hi, :integer
field :prcp, :float, default: 0.0
timestamps
end
end
Run Code Online (Sandbox Code Playgroud)
然后在"查询"部分
def sample_query do
query = from w in Weather,
where: w.prcp > 0.0 or is_nil(w.prcp),
select: w
Simple.Repo.all(query)
end
end
Run Code Online (Sandbox Code Playgroud)
ecto gona使用Weather中声明的模式形成查询
我的问题是我只想连接到现有数据库'TESTDB'并做一些SELECT,我不需要任何新的schmema来完成我的工作.可以在外地做吗?
当我创建自己的查询时
query = from w in tenant
Run Code Online (Sandbox Code Playgroud)
输入命令后 $ mix do deps.get, compile
错误告诉我 function tenant/0 undefined
tenant不是功能,它只是一张表TESTDB,我没有在任何地方声明
我想我只是迷失了自己
Jos*_*lim 10
您可以通过传递字符串来查询数据库中的任何表:
from p in "posts", where: p.id > 0
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您不需要定义任何架构,只需直接查询表即可.最后,您还可以直接执行SQL查询:
Ecto.Adapters.SQL.query(YourRepo, "SELECT $1", [1])
Run Code Online (Sandbox Code Playgroud)
但是,你失去了Ecto给你的大部分好处.
无论是否使用,都需要声明表的模式Ecto.我认为目前没有选择自动执行此操作.例如,您可以拥有以下内容:
defmodule Tenant do
use Ecto.Model
schema "tenant" do
field :id, :integer
field :name, :string
# and so on depending on the columns in your table
end
end
Run Code Online (Sandbox Code Playgroud)
然后呢 query = from w in Tenant, select: w