Elixir ecto连接到现有数据库 [英] Elixir ecto connect to an existing DB

查看:96
本文介绍了Elixir ecto连接到现有数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的链接中进行了一些研究之后

After doing some research in the link below

https://github.com/elixir-lang/ecto/tree/master/examples/simple

I

这里有总是声明为

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

,然后在查询部分

 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中声明的模式

ecto gona form a query using the schema declared in Weather

构成查询我的问题是我只想连接到现有数据库'TESTDB'并执行一些SELECT,我不需要任何新的模式即可完成工作

My question is that I just want to connect to an existing database 'TESTDB' and doing some SELECT, I don't need any new schmema to do my work. Is it possible to do it in ecto please?

当我创建自己的查询时,例如

When I create my own query like

query = from w in tenant

c> $ mix do deps.get,编译

after I input the command $ mix do deps.get, compile

该错误告诉我函数租户/ 0未定义

承租人不起作用,它只是 TESTDB ,我没有任何位置声明

tenant is not function, it just a table in TESTDB that I didn't declare anywhere

我想我只是失去了自己在外生

I think I just lost myself in ecto

推荐答案

无论是否使用 Ecto 创建表,都需要声明表的模式。我认为目前没有自动执行此操作的选项。因此,例如,您可以使用以下命令:

You need to declare the schema for your table whether you create it using Ecto or not. I don't think there's currently an option to do this automatically. So for example you can have something like:

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

然后在租户中从w执行 query =,选择:w

And then do query = from w in Tenant, select: w

这篇关于Elixir ecto连接到现有数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆