N
N
Nikolai Kupstas2017-10-05 08:59:49
PostgreSQL
Nikolai Kupstas, 2017-10-05 08:59:49

How to remove a row from the database on update to avoid conflict?

Actually I have a table which consists of fields

  • microservice_id
  • transport_id
  • username
  • password
  • is_default

and unique key (microservice_id, transport_id)
I am trying to make a request
UPDATE microservices_transports
SET
  transport_id = :next,
  username     = :username,
  password     = :password,
  is_default   = COUNT((SELECT *
                        FROM
                          (DELETE FROM microservices_transports AS mt WHERE mt.transport_id = :next AND mt.microservice_id = microservice_id RETURNING *)
                        WHERE is_default = TRUE
                       )) > 0
WHERE
  transport_id = :prev

But he swears at the syntax about DELETE. I don't know how else to do this kind of action. Are there any easier or better ways?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Pozdnyakov, 2017-10-05
@Afinogen

After FROM there should be a table name, and you have a deletion there that returns *

G
gill-sama, 2017-10-05
@gill-sama

What to make select from delete it is necessary that this delete returned something. You don't have it. You need returning in delete

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question