M
M
mmind2021-01-08 14:39:32
PostgreSQL
mmind, 2021-01-08 14:39:32

How to insert a function into a templated query string?

How to insert a function into a templated query string?
The created field is of type timezone. I want to convert the source data to this type using the postgre's TO_TIMESTAMP() function .
How to do it?

INSERT INTO data.source(created, created_utc) VALUES($1, $2)

If I insert the function directly into the values, then this error:
error: invalid input syntax for type timestamp with time zone: "TO_TIMESTAMP('1581569438')"

If I do this:
INSERT INTO data.source(created, created_utc) VALUES(TO_TIMESTAMP('$1'), TO_TIMESTAMP('$2'))

That error is the following:
duplicate key value violates unique constraint "source_pkey"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2021-01-08
@mmind

The second option is ok. They also write to you that the uniqueness is violated - there are already such (such) values ​​in the table. For which fields do you have the source_pkey constraint (this, apparently, is the primary key)?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question