title: Rails 6 pessimistic lock with jsonb
body: <p>I have a dedicated service used to push on an <code>jsonb</code> column timestamps about some events.</p>
  <p>Configuration:</p>
  <pre><code>pg: 1.2.2
  PostgreSQL: 11.4
  Rails: 6.0.2.1
  Ruby: 2.6.5
  </code></pre>
  <p>Here is the schema of my table:</p>
  <pre class="lang-rb prettyprint-override"><code>create_table &quot;invitations&quot;, force: :cascade do |t|
    t.uuid &quot;uuid&quot;, default: -&gt; { &quot;gen_random_uuid()&quot; }, null: false
    t.datetime &quot;created_at&quot;, precision: 6, null: false
    t.datetime &quot;updated_at&quot;, precision: 6, null: false
    t.jsonb &quot;notification_statuses&quot;, default: {}, null: false
  end
  </code></pre>
  <p>Here is the typical <code>jsonb</code> I have for the column <code>notification_statuses</code>:</p>
  <pre class="lang-json prettyprint-override"><code>{emails: [{created_at: &quot;some date&quot;, sent_at: &quot;some date2&quot;},
            {created_at: &quot;some other date&quot;, sent_at: &quot;some other date2&quot;}]}
  </code></pre>
  <p>Here is the code:</p>
  <pre class="lang-rb prettyprint-override"><code>class MyNotificationManager
  
    def initialize(invitation_id)
      @invitation = Invitation.find(invitation_id)
    end
  
    def start_new_notification_status(type)
      @invitation.reload.with_lock do
        @invitation.notification_statuses[type] = [] unless @invitation.notification_statuses.key?(type)
        hsh = { created_at: Time.zone.now, sent_at: nil }
        @invitation.notification_statuses[type].push(hsh)
        @invitation.save!
        puts &quot;push #{@invitation.notification_statuses[type].count}&quot;
      end
    end
  end
  </code></pre>
  <p>Here is the test:</p>
  <pre class="lang-rb prettyprint-override"><code>describe '#start_new_notification_status' do
    subject { MyNotificationManager }
  
    describe &quot;with concurrency&quot; do
      let(:concurrency_level) { 4 }
      let!(:invitation) { create(:invitation) }
  
      it &quot;updates invitation one at the time&quot; do
        expect(ActiveRecord::Base.connection.pool.size).to eq(5)
        fail_occurred = false
        wait_for_it = true
  
        threads = concurrency_level.times.map do |i|
          Thread.new do
            true while wait_for_it
            begin
              serv = subject.new(invitation.id)
              serv.start_new_notification_status('emails')
            rescue StandardError =&gt; err
              fail_occurred = true
            end
          end
        end
        wait_for_it = false
        threads.each(&amp;:join)
        expect(fail_occurred).not_to be_truthy
        expect(invitation.reload.notification_statuses['emails'].count).to eq(concurrency_level)
      end
    end
  end
  </code></pre>
  <p>Finally, my RSpec logs:</p>
  <pre><code>Event::NotificationManager
    #start_new_notification_status
      with concurrency
  push 1
  push 2
  push 2
  push 2
  
       Failure/Error: expect(invitation.reload.notification_statuses['emails'].count).to eq(concurrency_level)
  
         expected: 4
              got: 2
  </code></pre>
  <p>What am I missing? Logs should obviously be:</p>
  <pre><code>Event::NotificationManager
    #start_new_notification_status
      with concurrency
  push 1
  push 2
  push 3
  push 4
  </code></pre>
  <p>Here are my logs.</p>
  <pre><code>   (0.5ms)  SELECT &quot;ar_internal_metadata&quot;.&quot;value&quot; FROM &quot;ar_internal_metadata&quot; WHERE &quot;ar_internal_metadata&quot;.&quot;key&quot; = $1  [[&quot;key&quot;, &quot;schema_sha1&quot;]]
     (0.7ms)  SELECT &quot;schema_migrations&quot;.&quot;version&quot; FROM &quot;schema_migrations&quot; ORDER BY &quot;schema_migrations&quot;.&quot;version&quot; ASC
     (0.9ms)  BEGIN
     (0.3ms)  SAVEPOINT active_record_1
    Address Create (19.8ms)  INSERT INTO &quot;addresses&quot; (&quot;uuid&quot;, &quot;line1&quot;, &quot;line2&quot;, &quot;locality&quot;, &quot;zip_postcode&quot;, &quot;country_province&quot;, &quot;country&quot;, &quot;latitude&quot;, &quot;longitude&quot;, &quot;created_at&quot;, &quot;updated_at&quot;) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING &quot;id&quot;  [[&quot;uuid&quot;, &quot;6ca512f7-aa9d-4deb-9fce-7ce3e878f5ee&quot;], [&quot;line1&quot;, &quot;27862 Kautzer Dam&quot;], [&quot;line2&quot;, &quot;Apt. 831&quot;], [&quot;locality&quot;, &quot;West Gladys&quot;], [&quot;zip_postcode&quot;, &quot;27812-2854&quot;], [&quot;country_province&quot;, &quot;North Carolina&quot;], [&quot;country&quot;, &quot;Saint Kitts and Nevis&quot;], [&quot;latitude&quot;, 6.990987893090221], [&quot;longitude&quot;, -79.50793749705787], [&quot;created_at&quot;, &quot;2020-04-20 07:49:21.760116&quot;], [&quot;updated_at&quot;, &quot;2020-04-20 07:49:21.760116&quot;]]
     (0.3ms)  RELEASE SAVEPOINT active_record_1
     (0.3ms)  SAVEPOINT active_record_1
    Establishment Create (14.0ms)  INSERT INTO &quot;establishments&quot; (&quot;name&quot;, &quot;contact_name&quot;, &quot;description&quot;, &quot;note&quot;, &quot;insta_user_name&quot;, &quot;address_id&quot;, &quot;created_at&quot;, &quot;updated_at&quot;) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING &quot;id&quot;  [[&quot;name&quot;, &quot;Stehr-Donnelly&quot;], [&quot;contact_name&quot;, &quot;Narcisa&quot;], [&quot;description&quot;, &quot;Asperiores voluptates consequuntur.&quot;], [&quot;note&quot;, &quot;Dolore qui.&quot;], [&quot;insta_user_name&quot;, &quot;berta.king&quot;], [&quot;address_id&quot;, 1], [&quot;created_at&quot;, &quot;2020-04-20 07:49:21.792061&quot;], [&quot;updated_at&quot;, &quot;2020-04-20 07:49:21.792061&quot;]]
    ActsAsTaggableOn::Tagging Load (0.6ms)  SELECT &quot;taggings&quot;.* FROM &quot;taggings&quot; WHERE &quot;taggings&quot;.&quot;taggable_id&quot; = $1 AND &quot;taggings&quot;.&quot;taggable_type&quot; = $2  [[&quot;taggable_id&quot;, 1], [&quot;taggable_type&quot;, &quot;Establishment&quot;]]
     (0.2ms)  RELEASE SAVEPOINT active_record_1
     (0.3ms)  SAVEPOINT active_record_1
     (1.0ms)  SELECT COUNT(*) FROM &quot;events&quot; WHERE &quot;events&quot;.&quot;establishment_id&quot; = $1 AND ((start_at &lt;= '2020-04-22 16:05:59' AND end_at &gt;= '2020-04-22 16:05:59' AND end_at &lt;= '2020-04-22 18:05:59') OR
             (end_at &gt;= '2020-04-22 18:05:59' AND start_at &lt;= '2020-04-22 18:05:59' AND start_at &gt;= '2020-04-22 16:05:59') OR
              (start_at &gt;= '2020-04-22 16:05:59' AND end_at &lt;= '2020-04-22 18:05:59') OR
             (start_at &lt;= '2020-04-22 16:05:59' AND end_at &gt;= '2020-04-22 18:05:59'))  [[&quot;establishment_id&quot;, 1]]
    Event Create (17.1ms)  INSERT INTO &quot;events&quot; (&quot;uuid&quot;, &quot;establishment_id&quot;, &quot;name&quot;, &quot;start_at&quot;, &quot;end_at&quot;, &quot;max_female_guest&quot;, &quot;max_male_guest&quot;, &quot;created_at&quot;, &quot;updated_at&quot;, &quot;published&quot;) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING &quot;id&quot;  [[&quot;uuid&quot;, &quot;f110cba0-1f97-45ed-bc84-a8bebc73b8f8&quot;], [&quot;establishment_id&quot;, 1], [&quot;name&quot;, &quot;Clifford Kulas Conroy&quot;], [&quot;start_at&quot;, &quot;2020-04-22 16:05:59&quot;], [&quot;end_at&quot;, &quot;2020-04-22 18:05:59&quot;], [&quot;max_female_guest&quot;, 5], [&quot;max_male_guest&quot;, 5], [&quot;created_at&quot;, &quot;2020-04-20 07:49:21.830331&quot;], [&quot;updated_at&quot;, &quot;2020-04-20 07:49:21.830331&quot;], [&quot;published&quot;, true]]
    Establishment Update All (0.6ms)  UPDATE &quot;establishments&quot; SET &quot;events_count&quot; = COALESCE(&quot;events_count&quot;, 0) + $1 WHERE &quot;establishments&quot;.&quot;id&quot; = $2  [[&quot;events_count&quot;, 1], [&quot;id&quot;, 1]]
     (0.2ms)  RELEASE SAVEPOINT active_record_1
     (0.4ms)  SAVEPOINT active_record_1
    Area Create (8.1ms)  INSERT INTO &quot;areas&quot; (&quot;uuid&quot;, &quot;name&quot;, &quot;slug&quot;, &quot;created_at&quot;, &quot;updated_at&quot;) VALUES ($1, $2, $3, $4, $5) RETURNING &quot;id&quot;  [[&quot;uuid&quot;, &quot;e13cec8d-4767-490f-a909-f07970006743&quot;], [&quot;name&quot;, &quot;Derick Robel&quot;], [&quot;slug&quot;, &quot;derick-robel&quot;], [&quot;created_at&quot;, &quot;2020-04-20 07:49:21.970034&quot;], [&quot;updated_at&quot;, &quot;2020-04-20 07:49:21.970034&quot;]]
     (0.3ms)  RELEASE SAVEPOINT active_record_1
     (0.3ms)  SAVEPOINT active_record_1
    User Exists? (0.9ms)  SELECT 1 AS one FROM &quot;users&quot; WHERE &quot;users&quot;.&quot;email&quot; = $1 LIMIT $2  [[&quot;email&quot;, &quot;vernon@bergstrom.biz&quot;], [&quot;LIMIT&quot;, 1]]
    User Create (18.8ms)  INSERT INTO &quot;users&quot; (&quot;uuid&quot;, &quot;email&quot;, &quot;encrypted_password&quot;, &quot;confirmation_token&quot;, &quot;confirmed_at&quot;, &quot;confirmation_sent_at&quot;, &quot;created_at&quot;, &quot;updated_at&quot;) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING &quot;id&quot;  [[&quot;uuid&quot;, &quot;3bb5012a-ff5b-4dc8-b474-98aa06298c5c&quot;], [&quot;email&quot;, &quot;vernon@bergstrom.biz&quot;], [&quot;encrypted_password&quot;, &quot;$2a$04$dnmdv.wKESDVpFTn9UyqE.uFQwCexFGcmYBdg18HfGrvv0/4xrrQm&quot;], [&quot;confirmation_token&quot;, &quot;bfa804e9-cd89-49ba-acb5-3afcfd5425f6&quot;], [&quot;confirmed_at&quot;, &quot;2020-04-20 07:49:21.916321&quot;], [&quot;confirmation_sent_at&quot;, &quot;2020-04-20 06:49:21.916422&quot;], [&quot;created_at&quot;, &quot;2020-04-20 07:49:22.008360&quot;], [&quot;updated_at&quot;, &quot;2020-04-20 07:49:22.008360&quot;]]
    AreasUser Create (4.8ms)  INSERT INTO &quot;areas_users&quot; (&quot;area_id&quot;, &quot;user_id&quot;) VALUES ($1, $2)  [[&quot;area_id&quot;, 1], [&quot;user_id&quot;, 1]]
     (0.2ms)  RELEASE SAVEPOINT active_record_1
     (0.2ms)  SAVEPOINT active_record_1
    Consumer Create (8.4ms)  INSERT INTO &quot;consumers&quot; (&quot;user_id&quot;, &quot;first_name&quot;, &quot;last_name&quot;, &quot;whatsapp_user_name&quot;, &quot;phone_number&quot;, &quot;created_at&quot;, &quot;updated_at&quot;) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING &quot;id&quot;  [[&quot;user_id&quot;, 1], [&quot;first_name&quot;, &quot;Kermit&quot;], [&quot;last_name&quot;, &quot;Cremin&quot;], [&quot;whatsapp_user_name&quot;, &quot;7a206657-8365-4075-b4e8-b58d421ac2e2&quot;], [&quot;phone_number&quot;, &quot;+855 618.798.2641&quot;], [&quot;created_at&quot;, &quot;2020-04-20 07:49:22.036784&quot;], [&quot;updated_at&quot;, &quot;2020-04-20 07:49:22.036784&quot;]]
     (0.3ms)  RELEASE SAVEPOINT active_record_1
     (0.2ms)  SAVEPOINT active_record_1
    Guest Create (18.1ms)  INSERT INTO &quot;guests&quot; (&quot;uuid&quot;, &quot;gender&quot;, &quot;birthdate&quot;, &quot;profession&quot;, &quot;insta_user_name&quot;, &quot;consumer_id&quot;, &quot;created_at&quot;, &quot;updated_at&quot;) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING &quot;id&quot;  [[&quot;uuid&quot;, &quot;47d43976-e9a9-49d4-a722-22c62821d3b4&quot;], [&quot;gender&quot;, 1], [&quot;birthdate&quot;, &quot;1998-09-11 00:00:00&quot;], [&quot;profession&quot;, &quot;Global Sales Engineer&quot;], [&quot;insta_user_name&quot;, &quot;jeannetta.harber&quot;], [&quot;consumer_id&quot;, 1], [&quot;created_at&quot;, &quot;2020-04-20 07:49:22.049803&quot;], [&quot;updated_at&quot;, &quot;2020-04-20 07:49:22.049803&quot;]]
     (0.2ms)  RELEASE SAVEPOINT active_record_1
     (0.2ms)  SAVEPOINT active_record_1
    Invitation Create (12.8ms)  INSERT INTO &quot;invitations&quot; (&quot;guest_id&quot;, &quot;event_id&quot;, &quot;created_at&quot;, &quot;updated_at&quot;) VALUES ($1, $2, $3, $4) RETURNING &quot;id&quot;  [[&quot;guest_id&quot;, 1], [&quot;event_id&quot;, 1], [&quot;created_at&quot;, &quot;2020-04-20 07:49:22.071668&quot;], [&quot;updated_at&quot;, &quot;2020-04-20 07:49:22.071668&quot;]]
    Event Update All (0.5ms)  UPDATE &quot;events&quot; SET &quot;invitations_count&quot; = COALESCE(&quot;invitations_count&quot;, 0) + $1 WHERE &quot;events&quot;.&quot;id&quot; = $2  [[&quot;invitations_count&quot;, 1], [&quot;id&quot;, 1]]
     (0.2ms)  RELEASE SAVEPOINT active_record_1
    Invitation Load (0.5ms)  SELECT &quot;invitations&quot;.* FROM &quot;invitations&quot; WHERE &quot;invitations&quot;.&quot;id&quot; = $1 LIMIT $2  [[&quot;id&quot;, 1], [&quot;LIMIT&quot;, 1]]
    Invitation Load (1.2ms)  SELECT &quot;invitations&quot;.* FROM &quot;invitations&quot; WHERE &quot;invitations&quot;.&quot;id&quot; = $1 LIMIT $2  [[&quot;id&quot;, 1], [&quot;LIMIT&quot;, 1]]
    Invitation Load (0.3ms)  SELECT &quot;invitations&quot;.* FROM &quot;invitations&quot; WHERE &quot;invitations&quot;.&quot;id&quot; = $1 LIMIT $2  [[&quot;id&quot;, 1], [&quot;LIMIT&quot;, 1]]
     (3.3ms)  SAVEPOINT active_record_1
    Invitation Load (0.9ms)  SELECT &quot;invitations&quot;.* FROM &quot;invitations&quot; WHERE &quot;invitations&quot;.&quot;id&quot; = $1 LIMIT $2 FOR UPDATE  [[&quot;id&quot;, 1], [&quot;LIMIT&quot;, 1]]
    Guest Load (0.4ms)  SELECT &quot;guests&quot;.* FROM &quot;guests&quot; WHERE &quot;guests&quot;.&quot;id&quot; = $1 LIMIT $2  [[&quot;id&quot;, 1], [&quot;LIMIT&quot;, 1]]
    Event Load (0.4ms)  SELECT &quot;events&quot;.* FROM &quot;events&quot; WHERE &quot;events&quot;.&quot;id&quot; = $1 LIMIT $2  [[&quot;id&quot;, 1], [&quot;LIMIT&quot;, 1]]
    Invitation Update (0.7ms)  UPDATE &quot;invitations&quot; SET &quot;updated_at&quot; = $1, &quot;notification_statuses&quot; = $2 WHERE &quot;invitations&quot;.&quot;id&quot; = $3  [[&quot;updated_at&quot;, &quot;2020-04-20 07:49:22.111406&quot;], [&quot;notification_statuses&quot;, &quot;{\&quot;emails\&quot;:[{\&quot;created_at\&quot;:\&quot;2020-04-20T07:49:22.105Z\&quot;}]}&quot;], [&quot;id&quot;, 1]]
     (0.2ms)  RELEASE SAVEPOINT active_record_1
    Invitation Load (0.4ms)  SELECT &quot;invitations&quot;.* FROM &quot;invitations&quot; WHERE &quot;invitations&quot;.&quot;id&quot; = $1 LIMIT $2 FOR UPDATE  [[&quot;id&quot;, 1], [&quot;LIMIT&quot;, 1]]
    Invitation Load (0.3ms)  SELECT &quot;invitations&quot;.* FROM &quot;invitations&quot; WHERE &quot;invitations&quot;.&quot;id&quot; = $1 LIMIT $2  [[&quot;id&quot;, 1], [&quot;LIMIT&quot;, 1]]
     (0.2ms)  SAVEPOINT active_record_1
    Invitation Load (1.1ms)  SELECT &quot;invitations&quot;.* FROM &quot;invitations&quot; WHERE &quot;invitations&quot;.&quot;id&quot; = $1 LIMIT $2 FOR UPDATE  [[&quot;id&quot;, 1], [&quot;LIMIT&quot;, 1]]
    Guest Load (0.9ms)  SELECT &quot;guests&quot;.* FROM &quot;guests&quot; WHERE &quot;guests&quot;.&quot;id&quot; = $1 LIMIT $2  [[&quot;id&quot;, 1], [&quot;LIMIT&quot;, 1]]
    Event Load (0.3ms)  SELECT &quot;events&quot;.* FROM &quot;events&quot; WHERE &quot;events&quot;.&quot;id&quot; = $1 LIMIT $2  [[&quot;id&quot;, 1], [&quot;LIMIT&quot;, 1]]
    Invitation Update (0.7ms)  UPDATE &quot;invitations&quot; SET &quot;updated_at&quot; = $1, &quot;notification_statuses&quot; = $2 WHERE &quot;invitations&quot;.&quot;id&quot; = $3  [[&quot;updated_at&quot;, &quot;2020-04-20 07:49:22.129222&quot;], [&quot;notification_statuses&quot;, &quot;{\&quot;emails\&quot;:[{\&quot;created_at\&quot;:\&quot;2020-04-20T07:49:22.105Z\&quot;},{\&quot;created_at\&quot;:\&quot;2020-04-20T07:49:22.124Z\&quot;}]}&quot;], [&quot;id&quot;, 1]]
     (0.2ms)  RELEASE SAVEPOINT active_record_1
    Guest Load (0.3ms)  SELECT &quot;guests&quot;.* FROM &quot;guests&quot; WHERE &quot;guests&quot;.&quot;id&quot; = $1 LIMIT $2  [[&quot;id&quot;, 1], [&quot;LIMIT&quot;, 1]]
    Invitation Load (1.1ms)  SELECT &quot;invitations&quot;.* FROM &quot;invitations&quot; WHERE &quot;invitations&quot;.&quot;id&quot; = $1 LIMIT $2 FOR UPDATE  [[&quot;id&quot;, 1], [&quot;LIMIT&quot;, 1]]
     (2.1ms)  SAVEPOINT active_record_1
    Invitation Update (2.7ms)  UPDATE &quot;invitations&quot; SET &quot;updated_at&quot; = $1, &quot;notification_statuses&quot; = $2 WHERE &quot;invitations&quot;.&quot;id&quot; = $3  [[&quot;updated_at&quot;, &quot;2020-04-20 07:49:22.136369&quot;], [&quot;notification_statuses&quot;, &quot;{\&quot;emails\&quot;:[{\&quot;created_at\&quot;:\&quot;2020-04-20T07:49:22.105Z\&quot;},{\&quot;created_at\&quot;:\&quot;2020-04-20T07:49:22.124Z\&quot;},{\&quot;created_at\&quot;:\&quot;2020-04-20T07:49:22.135Z\&quot;}]}&quot;], [&quot;id&quot;, 1]]
     (0.5ms)  RELEASE SAVEPOINT active_record_1
    Invitation Load (4.1ms)  SELECT &quot;invitations&quot;.* FROM &quot;invitations&quot; WHERE &quot;invitations&quot;.&quot;id&quot; = $1 LIMIT $2  [[&quot;id&quot;, 1], [&quot;LIMIT&quot;, 1]]
     (1.0ms)  ROLLBACK
     (0.3ms)  BEGIN
     (3.4ms)  ALTER TABLE &quot;tags&quot; DISABLE TRIGGER ALL;ALTER TABLE &quot;schema_migrations&quot; DISABLE TRIGGER ALL;ALTER TABLE &quot;ar_internal_metadata&quot; DISABLE TRIGGER ALL;ALTER TABLE &quot;areas_establishments&quot; DISABLE TRIGGER ALL;ALTER TABLE &quot;consumer_ratings&quot; DISABLE TRIGGER ALL;ALTER TABLE &quot;addresses&quot; DISABLE TRIGGER ALL;ALTER TABLE &quot;oauth_access_tokens&quot; DISABLE TRIGGER ALL;ALTER TABLE &quot;guests&quot; DISABLE TRIGGER ALL;ALTER TABLE &quot;areas_users&quot; DISABLE TRIGGER ALL;ALTER TABLE &quot;active_storage_blobs&quot; DISABLE TRIGGER ALL;ALTER TABLE &quot;areas&quot; DISABLE TRIGGER ALL;ALTER TABLE &quot;invitations&quot; DISABLE TRIGGER ALL;ALTER TABLE &quot;establishments_managers&quot; DISABLE TRIGGER ALL;ALTER TABLE &quot;consumers&quot; DISABLE TRIGGER ALL;ALTER TABLE &quot;events&quot; DISABLE TRIGGER ALL;ALTER TABLE &quot;taggings&quot; DISABLE TRIGGER ALL;ALTER TABLE &quot;users&quot; DISABLE TRIGGER ALL;ALTER TABLE &quot;oauth_applications&quot; DISABLE TRIGGER ALL;ALTER TABLE &quot;establishments&quot; DISABLE TRIGGER ALL;ALTER TABLE &quot;active_storage_attachments&quot; DISABLE TRIGGER ALL;ALTER TABLE &quot;oauth_access_grants&quot; DISABLE TRIGGER ALL;ALTER TABLE &quot;managers&quot; DISABLE TRIGGER ALL;ALTER TABLE &quot;devices&quot; DISABLE TRIGGER ALL;ALTER TABLE &quot;admins&quot; DISABLE TRIGGER ALL
     (5.0ms)  COMMIT
     (2.3ms)            SELECT schemaname || '.' || tablename
            FROM pg_tables
            WHERE
              tablename !~ '_prt_' AND
               tablename &lt;&gt; 'schema_migrations'  AND tablename &lt;&gt; 'ar_internal_metadata'  AND
              schemaname = ANY (current_schemas(false))
  
     (1.7ms)  select table_name from information_schema.views where table_schema = 'secret_core_test'
     (1425.7ms)  TRUNCATE TABLE &quot;public&quot;.&quot;tags&quot;, &quot;public&quot;.&quot;areas_establishments&quot;, &quot;public&quot;.&quot;consumer_ratings&quot;, &quot;public&quot;.&quot;addresses&quot;, &quot;public&quot;.&quot;oauth_access_tokens&quot;, &quot;public&quot;.&quot;guests&quot;, &quot;public&quot;.&quot;areas_users&quot;, &quot;public&quot;.&quot;active_storage_blobs&quot;, &quot;public&quot;.&quot;areas&quot;, &quot;public&quot;.&quot;invitations&quot;, &quot;public&quot;.&quot;establishments_managers&quot;, &quot;public&quot;.&quot;consumers&quot;, &quot;public&quot;.&quot;events&quot;, &quot;public&quot;.&quot;taggings&quot;, &quot;public&quot;.&quot;users&quot;, &quot;public&quot;.&quot;oauth_applications&quot;, &quot;public&quot;.&quot;establishments&quot;, &quot;public&quot;.&quot;active_storage_attachments&quot;, &quot;public&quot;.&quot;oauth_access_grants&quot;, &quot;public&quot;.&quot;managers&quot;, &quot;public&quot;.&quot;devices&quot;, &quot;public&quot;.&quot;admins&quot; RESTART IDENTITY CASCADE;
     (0.5ms)  BEGIN
     (2.4ms)  ALTER TABLE &quot;tags&quot; ENABLE TRIGGER ALL;ALTER TABLE &quot;schema_migrations&quot; ENABLE TRIGGER ALL;ALTER TABLE &quot;ar_internal_metadata&quot; ENABLE TRIGGER ALL;ALTER TABLE &quot;areas_establishments&quot; ENABLE TRIGGER ALL;ALTER TABLE &quot;consumer_ratings&quot; ENABLE TRIGGER ALL;ALTER TABLE &quot;addresses&quot; ENABLE TRIGGER ALL;ALTER TABLE &quot;oauth_access_tokens&quot; ENABLE TRIGGER ALL;ALTER TABLE &quot;guests&quot; ENABLE TRIGGER ALL;ALTER TABLE &quot;areas_users&quot; ENABLE TRIGGER ALL;ALTER TABLE &quot;active_storage_blobs&quot; ENABLE TRIGGER ALL;ALTER TABLE &quot;areas&quot; ENABLE TRIGGER ALL;ALTER TABLE &quot;invitations&quot; ENABLE TRIGGER ALL;ALTER TABLE &quot;establishments_managers&quot; ENABLE TRIGGER ALL;ALTER TABLE &quot;consumers&quot; ENABLE TRIGGER ALL;ALTER TABLE &quot;events&quot; ENABLE TRIGGER ALL;ALTER TABLE &quot;taggings&quot; ENABLE TRIGGER ALL;ALTER TABLE &quot;users&quot; ENABLE TRIGGER ALL;ALTER TABLE &quot;oauth_applications&quot; ENABLE TRIGGER ALL;ALTER TABLE &quot;establishments&quot; ENABLE TRIGGER ALL;ALTER TABLE &quot;active_storage_attachments&quot; ENABLE TRIGGER ALL;ALTER TABLE &quot;oauth_access_grants&quot; ENABLE TRIGGER ALL;ALTER TABLE &quot;managers&quot; ENABLE TRIGGER ALL;ALTER TABLE &quot;devices&quot; ENABLE TRIGGER ALL;ALTER TABLE &quot;admins&quot; ENABLE TRIGGER ALL
     (1.8ms)  COMMIT
  </code></pre>
  <p><strong>EDIT:</strong> It seems that not all SQL updates are generated. I've the exact same number of not stored data on my array than missing SQL updates. Even if <code>@invitation.save!</code> always returns true (I've checked that right now).</p>
  <p>**EDIT2: ** Sometime I have the error <code>&lt;RuntimeError: Locking a record with unpersisted changes is not supported. Use </code>save<code>to persist the changes, or</code>reload<code> to discard them explicitly.</code> But I have a <code>.reload</code> right before <code>.with_lock!</code>. How is this error possible? </p>