我们在Rails中使用包含或其他类似的连接方法(或SQL);
那么函数在Elixir(Ecto)中解决了这个问题?
我可以time_stamp在最后一行返回而不用大括号提取它吗?
{:ok, time_stamp} = Myapp.Repo.insert(changeset) # |> Map.get time_stamp
time_stamp
# {:ok, %Myapp.TimeStamp{__meta__: #Ecto.Schema.Metadata<:loaded>, active: true...}
Run Code Online (Sandbox Code Playgroud) 我还有一个新手问题。当我做类似的事情
case MyRepo.insert %Post{title: "Ecto is great"} do
{:ok, struct} -> # Inserted with success
{:error, changeset} -> # Something went wrong
end
Run Code Online (Sandbox Code Playgroud)
Repo如何知道要使用数据库中的哪个表?
如何在架构中添加功能?我想创建用于动态地将字段添加到模型模式的函数.例:
def func do
# .. loop to create dynamic fields
field :street, :string
end
schema "objects" do
func
end
... Error:
** (CompileError) web/models/objects.ex:12: undefined function func/0
Run Code Online (Sandbox Code Playgroud) 我发现bullet gem是一个非常有用的工具,用于在Rails应用程序中捕获慢速数据库查询.是否有类似的工具Ecto/ Phoenix有助于通知您N + 1和其他慢查询?
让我们假设我有一张桌子"customers".我想获得该表的外键.
我们可以使用model.__ struct .__ meta` 从模型中获取表名
我们还可以通过加载所有模块从表中获取模型名称,并使模型模式与表名匹配
这可能是我们从表中获取外键吗?
这样做的最佳方法是什么?
如果它可能在ecto?
谢谢.
我只是尝试使用Postgrex而没有任何类型的ecto设置,所以只是文档自述文件中的示例.
这是我的模块的样子:
defmodule Receive do
def start(_type, _args) do
{:ok, pid} = Postgrex.start_link(
hostname: "localhost",
username: "john",
# password: "",
database: "property_actions",
extensions: [{Postgrex.Extensions.JSON}]
)
Postgrex.query!(
pid,
"INSERT INTO actions (search_terms) VALUES ($1)",
[
%{foo: 'bar'}
]
)
end
end
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,我得到了
** (RuntimeError) type `json` can not be handled by the types module Postgrex.DefaultTypes, it must define a `:json` library in its options to support JSON types
Run Code Online (Sandbox Code Playgroud)
有什么我没有正确设置?根据我在文档中收集的内容,我甚至不需要具有该扩展行,因为默认情况下处理json.
我有一个查询函数,它给出utc_date_from和utc_date_to并取 的和total。
由于我需要提供本地日期,因此我将time_period本地日期添加到 中select。
time_period = to_string(date)
Payment
|> where([p], p.core_id == ^core_id and
p.inserted_at >= datetime_add(^utc_date_from, 0, "day") and
p.inserted_at <= datetime_add(^utc_date_to, 0, "day"))
|> select([p], %{amount: sum(p.total), time_period: ^time_period})
|> Repo.one()
Run Code Online (Sandbox Code Playgroud)
但是,如果我添加time_period到select,它会触发一个错误(Postgrex.Error) ERROR 42P18 (indeterminate_datatype): could not determine data type of parameter $1。
此外,此错误仅在我在 ubuntu 中运行我的应用程序时显示。如果我尝试使用 mac os,它不会显示此错误。
这个函数有什么问题吗?我怎样才能让它在ubuntu上工作?
嘿,我正在尝试进行查询,我的代码如下所示:
def show(conn, _params) do
user = Guardian.Plug.current_resource(conn)
team = Web.get_team!(user.team.id)
score_query =
from(
u in User,
where: u.team.id == team.id,
select: sum(u.score)
)
team_score = Repo.all(score_query)
IO.puts("score")
IO.inspect(team_score)
conn |> render("team.json", team: team)
Run Code Online (Sandbox Code Playgroud)
当我尝试运行它时,我收到一条错误消息:
** (Ecto.Query.CompileError) unbound variable `team` in query
Run Code Online (Sandbox Code Playgroud)
但为什么是未绑定的?我该如何修复它以及为什么会发生这种情况?
我想像下面的 SQL 一样用 ecto 计算 2 列值
SELECT goal, assist, (goal + assist) as point
FROM game
Run Code Online (Sandbox Code Playgroud)
我想添加带有目标+辅助列的点列。请给我一个建议。
当我尝试使用Ecto以下方法在我的数据库中插入一行时:
sla = %MyMetrics.MonthlySLA{metric_name: "x", metric_type: "y", value: 43.33, metric_date: Date.new!(2020,1,1)}
MyMetrics.Repo.insert(sla)
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
17:44:26.704 [debug] QUERY ERROR db=134.8ms queue=143.8ms idle=1397.2ms
INSERT INTO `monthly_slas` (`metric_date`,`metric_name`,`metric_type`,`value`) VALUES (?,?,?,?) [~D[2020-01-01], "x", "y", #Decimal<43.33>]
** (MyXQL.Error) (1364) (ER_NO_DEFAULT_FOR_FIELD) Field 'inserted_at' doesn't have a default value
(ecto_sql 3.5.3) lib/ecto/adapters/myxql.ex:241: Ecto.Adapters.MyXQL.insert/6
(ecto 3.5.5) lib/ecto/repo/schema.ex:649: Ecto.Repo.Schema.apply/4
(ecto 3.5.5) lib/ecto/repo/schema.ex:262: anonymous fn/15 in Ecto.Repo.Schema.do_insert/4
Run Code Online (Sandbox Code Playgroud)
我的Ecto模型。
defmodule MyMetrics.MonthlySLA do
use Ecto.Schema
import Ecto.Changeset
schema "monthly_slas" do
field :metric_name, :string
field :metric_type, :string
field :metric_date, :date …Run Code Online (Sandbox Code Playgroud) 我在 Elixir 中有一个非常简单的应用程序,它使用 Ecto 在 Postgres 上的表中运行 SELECT 查询。\n表名称是“person”,有 2 列:“name”和“age”。
\n这是应用程序:
\n/ecto_app\n |-application.ex\n |-repo.ex\nRun Code Online (Sandbox Code Playgroud)\n应用程序.ex:
\ndefmodule EctoApp.Application do\n\n use Application\n\n require Logger\n\n @impl true\n def start(_type, _args) do\n children = [\n EctoApp.Repo\n ]\n\n opts = [strategy: :one_for_one, name: EctoApp.Supervisor]\n Logger.debug("Starting Ecto App")\n Supervisor.start_link(children, opts)\n end\nend\nRun Code Online (Sandbox Code Playgroud)\n回购协议:
\ndefmodule EctoApp.Repo do\n use Ecto.Repo,\n otp_app: :ecto_app,\n adapter: Ecto.Adapters.Postgres\n\n import Ecto.Query\n\n require Logger\n\n\n def get_people() do\n query = from p in "person",\n select: [p.name, p.age]\n result= EctoApp.Repo.all(query)\n …Run Code Online (Sandbox Code Playgroud) 我想Ecto使用自定义SQL代码生成新的迁移,而不是使用提供的帮助程序.当前的mix生成器任务仅处理模型.
如何在迁移中编写原始SQL代码?
ecto ×13
elixir ×13
postgresql ×2
json ×1
phoenix ×1
postgrex ×1
sql ×1
timestamp ×1
unit-testing ×1