saialluru

saialluru

Not sufficient documentation on using Postgres range types

Using raw SQL insert and update into range types does not work

updateSql = “UPDATE " <> type.namespace <> “_” <> type.predicate <> " SET
validity = int4range(lower(validity)::integer, $1::integer, ‘[)’)
where subject = $2 and validity @> $3::integer”

insetSql = "insert into range_table values ($1, '[$2::integer,)'::int4range, $3);"

Repo.transaction fn ->
    SQL.query(Repo, updateSql, [intvalue, string value, intvalue])
    SQL.query(Repo, insetSql, [stringvalue, intvalue, stringvalue], [])
end

The output does not clearly say what the error is
iex(23)> Postgres.store_sentence(sainame)
[debug] QUERY OK db=0.2ms idle=1404.9ms
begin
[debug] QUERY ERROR db=0.0ms
insert into default_name values ($1, ‘[$2::integer,)’::int4range, $3); [“Sai”, 0, “Alluru”]
[debug] QUERY OK db=0.2ms
rollback

First Post! Switch mode

ruslandoga

ruslandoga

Can you post the query you want to execute here? From what I see, you have a typo in the range definition, [$2::integer,)::int4range: [$2::integer,) is not a valid range.

Off-topic, I’d suggest not using raw sql in Repo.query, and instead going with Repo.insert_all. What you are trying to do can be done with insert_all in a safer and more idiomatic way.

Most Liked

ruslandoga

ruslandoga

Note that you posted [$2::integer,)::int4range in OP, [$2::integer,) is not a valid range.

In insert_all you can use Postgrex.Range — Postgrex v0.22.2.

Your raw SQL would be roughly equivalent to

validity = %Postgrex.Range{lower: 0, upper: :unbound, upper_inclusive: false}
Repo.insert_all("default_name", [%{subject: "Sai", validity: validity, object: "Alluru"}])
ruslandoga

ruslandoga

Check the response from Repo.query, it would contain the error reason. The logs you posted only show the executed queries, how long it took, and their status, they don’t show responses as it would be noisy.

ruslandoga

ruslandoga

You can probably use a join if the data to be updated is stored in some table:

-- adapted from https://www.postgresqltutorial.com/postgresql-update-join/
UPDATE table_name
  SET validity = int4range(lower(validity), other_table.value, '[)')
  FROM other_table
  WHERE table_name.key = other_table.key;

or if you don’t have a table like that and all your data comes from the app, you might be able to use with_cte/2 in which you’d define the records to be updated and then in the main body you’d run a query similar to the one above:

WITH updates (key, value) AS (
  VALUES (1, 2), (3, 4)
)
UPDATE table_name
  SET validity = int4range(lower(validity), updates.value, '[)')
  FROM updates
  WHERE table_name.key = updates.key;

There are other approaches as well, but that’s not Ecto specific, to learn more I’d suggest lurking / asking questions on postgres-specific forums, people there would be more knowledgeable about this type of questions and would probably provide you with better answers.

Where Next?

Trending in Announcing Top

bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
385 14863 120
New
JesseHerrick
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
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
zachdaniel
Introducing AshStorage! Attachment and file management that slots directly into your resources :smiling_face_with_sunglasses: I had hope...
New

Other Trending Topics Top

type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
bjorng
We want to introduce a new native datatype to Erlang: native records. Although replacing all tuple records with native records is not our...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
yureehuh
Introduction Founded in 2017 by landscape ecologist and fire mitigation expert Harry Statter, Frontline developed the first fully integra...
New

We're in Beta

About us Mission Statement