gpt4 book ai didi

postgis - 如何使用 Geo 库创建有效的 Ecto 模型变更集?

转载 作者:行者123 更新时间:2023-12-01 22:24:45 25 4
gpt4 key购买 nike

我正在尝试使用 Geo 库通过 Phoenix 模型变更集存储 Geo.Point。我的参数是: {coordinates: [49.44, 17.87]} 或更喜欢的是 {coordinates: {latitude: 49.44, longitude: 17.87}}

在 iex 控制台中我尝试过:

iex(5)> changeset = Place.changeset(%Place{}, %{coordinates: [49.44, 17.87]})
%Ecto.Changeset{action: nil, changes: %{}, constraints: [],
errors: [coordinates: "is invalid"], filters: %{}
model: %Myapp.Place{__meta__: #Ecto.Schema.Metadata<:built>,
coordinates: nil, id: nil, inserted_at: nil, updated_at: nil}, optional: [],
opts: [], params: %{"coordinates" => [49.445614899999995, 17.875574099999998]},
repo: nil, required: [:coordinates],

所有其他尝试均因 Poison.Parser 错误而结束。

应该如何从客户端查看参数来创建有效的变更集?

型号:

defmodule MyApp.Place do
use MyApp.Web, :model

schema "place" do
field :coordinates, Geo.Point

timestamps
end

@required_fields ~w(coordinates)
@optional_fields ~w()

def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
end
end

最佳答案

根据库的测试:

https://github.com/bryanjos/geo/blob/351ee6c4f8ed24541c9c2908f615e7b0a238f010/test/geo/ecto_test.exs#L100

您需要将 Geo.Point 传递给您的变更集函数:

changeset = Place.changeset(%Place{}, %{coordinates: %Geo.Point{coordinates: {49.44, 17.87}})

您可以在[文档]中阅读有关自定义 ecto 类型的更多信息。( https://hexdocs.pm/ecto/Ecto.Type.html#content )

关于postgis - 如何使用 Geo 库创建有效的 Ecto 模型变更集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33870732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com