Elixir ecto 连接到现有数据库
Elixir ecto connect to an existing DB
在下面的 link 中做了一些研究后
https://github.com/elixir-lang/ecto/tree/master/examples/simple
我对如何在 elixir 中使用 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
然后在 'Query' 部分
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
ecto gona 使用 Weather
中声明的模式形成查询
我的问题是 我只想连接到现有数据库 'TESTDB' 并做一些 SELECT,我不需要任何新的模式来完成我的工作。请问可以在体外做吗?
当我创建自己的查询时
query = from w in tenant
输入指令后$ mix do deps.get, compile
错误告诉我function tenant/0 undefined
tenant
不是函数,它只是 TESTDB
中的一个 table,我没有在任何地方声明
我想我只是迷失了自我
无论您是否使用 Ecto
创建它,您都需要为您的 table 声明架构。我认为目前没有自动执行此操作的选项。所以例如你可以有这样的东西:
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
然后query = from w in Tenant, select: w
您可以通过传递一个字符串来查询数据库中的任何table:
from p in "posts", where: p.id > 0
在这种情况下,您不需要定义任何架构,直接查询 table 即可。最后,你也可以直接做一个SQL查询:
Ecto.Adapters.SQL.query(YourRepo, "SELECT ", [1])
但是你会失去 Ecto 给你的大部分好处。
在下面的 link 中做了一些研究后
https://github.com/elixir-lang/ecto/tree/master/examples/simple
我对如何在 elixir 中使用 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
然后在 'Query' 部分
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
ecto gona 使用 Weather
中声明的模式形成查询我的问题是 我只想连接到现有数据库 'TESTDB' 并做一些 SELECT,我不需要任何新的模式来完成我的工作。请问可以在体外做吗?
当我创建自己的查询时
query = from w in tenant
输入指令后$ mix do deps.get, compile
错误告诉我function tenant/0 undefined
tenant
不是函数,它只是 TESTDB
中的一个 table,我没有在任何地方声明
我想我只是迷失了自我
无论您是否使用 Ecto
创建它,您都需要为您的 table 声明架构。我认为目前没有自动执行此操作的选项。所以例如你可以有这样的东西:
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
然后query = from w in Tenant, select: w
您可以通过传递一个字符串来查询数据库中的任何table:
from p in "posts", where: p.id > 0
在这种情况下,您不需要定义任何架构,直接查询 table 即可。最后,你也可以直接做一个SQL查询:
Ecto.Adapters.SQL.query(YourRepo, "SELECT ", [1])
但是你会失去 Ecto 给你的大部分好处。