Elixir ecto连接到现有数据库

编程入门 行业动态 更新时间:2024-10-24 06:27:41
本文介绍了Elixir ecto连接到现有数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

After doing some research in the link below

github/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连接到现有数据库

本文发布于:2023-10-17 13:01:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1500984.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:连接到   数据库   Elixir   ecto

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!