KP123
I have a time table that stores a time zone. I want to ensure that the value entered is a valid time zone. To do that I have a check constraint that attempts to cast the current time to the submitted tz. When using real tzs like “America/New_York” everything works expected. When I use the fake, “New_York”, tz insert fails as expected but Ecto.Changeset.check_constraint/3 fails to catch the resulting Postgrex.Error.
# Migration
def change do
create table(:times) do
add :time_zone, :string, null: false
end
create constraint(:times, :valid_time_zone, check: "now() at time zone time_zone is not null")
end
# Schema
defmodule MyApp.Time do
use Ecto.Schema
import Ecto.Changeset
schema "times" do
field :time_zone, :string
end
def changeset(time, attrs) do
time
|> cast(attrs, [:time_zone])
|> validate_required([:time_zone])
|> check_constraint(:time_zone, name: :valid_time_zone)
end
end
# Console Error
** (Postgrex.Error) ERROR 22023 (invalid_parameter_value) time zone "New_York" not recognized
(ecto_sql 3.11.1) lib/ecto/adapters/sql.ex:1054: Ecto.Adapters.SQL.raise_sql_call_error/1
(ecto 3.11.2) lib/ecto/repo/schema.ex:775: Ecto.Repo.Schema.apply/4
(ecto 3.11.2) lib/ecto/repo/schema.ex:377: anonymous fn/15 in Ecto.Repo.Schema.do_insert/4
(ecto 3.11.2) lib/ecto/repo/schema.ex:1033: anonymous fn/3 in Ecto.Repo.Schema.wrap_in_transaction/6
(ecto_sql 3.11.1) lib/ecto/adapters/sql.ex:1358: anonymous fn/3 in Ecto.Adapters.SQL.checkout_or_transaction/4
(db_connection 2.6.0) lib/db_connection.ex:1710: DBConnection.run_transaction/4
iex:3: (file)
Why does the check constraint fail here?
How should I handle raw raw Postgrex errors (using try?)
Is there a better way to test for timezone in a check constraint?
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
03juan
It looks like the check is failing because Postgres is trying to parse the date/time input and throwing an error before it gets to the
is not nullcheck.Have you confirmed that the psql statement
SELECT now() AT TIME ZONE 'New_York';does return null when providing an invalid time zone?Edit: Looks like it is a parse error in this case, see step 2a in the datetime input rules .
Probably nothing straightforward directly in a check constraint, but you could have your application code validate the
time_zoneinput against thepg_timezone_namesview before creating or editing theTimeentry.KP123
The parse error is intentional as its the best way to know if my string is a time zone. If it parses, it’s a tz, if it doesn’t, it’s not one. I do see time zone handled in a similar way here and here.
Postgres responding with
(invalid_parameter_value) time zone "New_York" not recognizedis enough information for me to know that the data provided is invalid. What I need is a way to catch that specific postgrex error. Im going to try using a try/rescue but I was hoping for a solution that would allow me to treat this error like a constraint so that I can handle it directly in a changeset.This stack overflow suggests manually catching and rethrowing errors as a constraint error, then catching them by overriding query functions in the Repo module, but that seems so heavy handed to handle the insert of a single column of a single entity only some of the time.
KP123
My solution for the time being:
The downside is that every
invalid_parameter_valueis treated as an invalid time zone. That’s fine for now as this is the only place that error can occur, but it’s not ideal for when the project grows and the table becomes more complex03juan
You could also write a db function to do the view tz name lookup and use it in the check
KP123
thanks