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 []
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.
The table I have is
CREATE TABLE IF NOT EXISTS public.default_name
(
subject character(15) COLLATE pg_catalog.“default” NOT NULL,
validity int4range,
object character(15) COLLATE pg_catalog.“default”,
CONSTRAINT default_name_subject_validity_excl EXCLUDE USING gist (
subject WITH =,
validity WITH &&)
)
The samples I want to execute using SQL are
insert into default_name values (‘Sai’, ‘[0,)’::int4range, ‘Alluru’);
followed by an update
UPDATE default_name SET
validity = int4range(lower(validity), 10, ‘[)’)
where subject = ‘Sai’ and validity @> 10
This works fine in Postgres unable figure out how to get this working on Using Repo.SQL
I am not using Ecto Schema I need tables to be created a run time, so want to just use plain SQL with prepared statements. By the I am very new to Elixir and the tools.
For update I just want to update the upper bound, I have no idea about the lower bound at that time, so do I not specify the lower in the Range and only specify upper., Would that work ?
UPDATE default_name SET
validity = int4range(lower(validity), 0, ‘[)’)
where subject = ‘Sai’ and validity @> 0; which retains the current lower. Looks like this is not possible with Repo.update_all()
I tired the following and it worked
from(p in table_name, where: p.subject == ^subject and fragment(“validity @> ?::integer”, ^scn),
update: [set: [validity: fragment(“int4range(lower(validity), ?, ‘[)’)”, ^scn)]])
|> Repo.update_all([])
However I am not sure how I can convert the above into batching, it seems like I have to do this for one record at a time, where as the insert below I can provide multiple records in the list and they will be applied as part of a batched prepared statement. Any suggestions I can convert this into a prepared statement batch for update.
Repo.insert_all(table_name, [record, record1])
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.
You can express batch update with update_all the same way you’d express it with raw sql. If you post the SQL query you want to execute, I can help you translate it into ecto functions/macros.