public inbox for [email protected]  
help / color / mirror / Atom feed
From: hubert depesz lubaczewski <[email protected]>
To: Hui Jackson <[email protected]>
Cc: [email protected]
Subject: Re: Using Transaction with if-else in prepared statement
Date: Thu, 26 May 2022 01:35:56 +0200
Message-ID: <[email protected]> (raw)
In-Reply-To: <CAHXAyjx-2MhO0zoCt3Vr_2AW6sFVjN0tgDo_GPVg=DVCzQDQMQ@mail.gmail.com>
References: <CAHXAyjx-2MhO0zoCt3Vr_2AW6sFVjN0tgDo_GPVg=DVCzQDQMQ@mail.gmail.com>

On Wed, May 25, 2022 at 07:12:49PM +0800, Hui Jackson wrote:
> I am trying to make transaction in nodejs
> The logic will be check if sufficient coin in sender's amount, if
> sufficient then do transaction.
> I am new to postgres, not sure if this is a right way to do so, if you have
> a better solution, please let me know, thank you.

I would do i by issuing *only*
INSERT INTO coin_history(sender_id, receiver_id, amount) VALUES
and then changing coin data with triggers plus adding constraints that
app_user.coin can never be < 0.

depesz





view thread (4+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected]
  Subject: Re: Using Transaction with if-else in prepared statement
  In-Reply-To: <[email protected]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox