blacklotus's avatar

Inner Join Increments Works in MySQL but not SQLite

I have the following codes that does cross tables increments when executed. When being tested against MySQL, they work perfectly. But they don't work with SQLite and that creates problems with my test cases.

Any advice if there are simple workarounds to have the same query / outcome that work on MySQL and SQLite?

DB::table('entries')
    ->join('transactions', function ($join) {
        $join->on('entries.transaction_id', 'transactions.id')
            ->where('transactions.batch_id', $batchId)
            ->where('entries.number', $number);
    })
    ->update([
        'entries.prize' => $position,
        'entries.winnings' => DB::raw("entries.amount * {$reward}"),
        'transactions.winnings' => DB::raw("transactions.winnings + (entries.amount * {$reward})"),
    ]);

They translate to these in SQL:

update
  `entries`
  inner join `transactions` on `entries`.`transaction_id` = `transactions`.`id`
  and `transactions`.`batch_id` = 4
  and `entries`.`number` = "2010"
set
  `entries`.`prize` = "consolation",
  `entries`.`winnings` = entries.amount * 500,
  `transactions`.`winnings` = transactions.winnings + (entries.amount * 500)
0 likes
1 reply
blacklotus's avatar

To answer my own question, the error is due to SQLite not supporting UPDATE JOIN operations. Thus there is no way around this except to break the operations into distinct steps, which was what I did.

Please or to participate in this conversation.