Spec-Zone.ru › Phoenix

Duration Types with Postgrex

Source

As of Ecto 3.12.0, Ecto supports a :duration type which maps to Elixir's Duration struct (available as of Elixir 1.17).

One natural use case for this is when using Postgres's interval type. Historically, Postgrex loads intervals from the database into a custom Postgrex.Interval struct. With the introduction of Duration, there is now the option to choose between the two. Please follow the steps below to enable mapping to Duration.

  1. Define your migration
create table("movies") do
  add :running_time, :interval
end
  1. Define your schema
defmodule Movie do
  use Ecto.Schema

  schema "movies" do
    field :running_time, :duration
  end
end
  1. Define your custom Postgrex type module and specify intervals should decode to Duration
# Inside lib/my_app/postgrex_types.ex

Postgrex.Types.define(MyApp.PostgrexTypes, [], interval_decode_type: Duration)
  1. Make Ecto aware of the Postgrex type module in your configuration
config :my_app, MyApp.Repo, types: MyApp.PostgresTypes
← Previous Page Data mapping and validation
Next Page → Dynamic queries

Hex Package Hex Preview (current file) View llms.txt Download ePub version

Built using ExDoc (v0.40.2) for the Elixir programming language

© 2014 Chris McCord
Licensed under the MIT License.
https://ecto.hexdocs.pm/duration-types-with-postgrex.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API