Skip to content

Write query attempted while in readonly mode: INSERT INTO "sessions #185

@jamienourish

Description

@jamienourish

Since Rails 6 we have been trying to work out what was creating errors such as
Write query attempted while in readonly mode: INSERT INTO "sessions" ("session_id", "data", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"

I have spent time today and have found that this gem writes raw sql using the bypass class
https://github.com/rails/activerecord-session_store/blob/b005852ea0460126bb4edd64ac03b78fbf244ead/lib/active_record/session_store/sql_bypass.rb

As the GET page request by default will use the reader DB's this is what was causing the error.

To get round this as a fix in my project I have had to create a class as the session and override save and destroy, wrapping in writer db, please not my write role is called "Writer" if you are having the same problem, please use your role name.

class ForceWriterSessionClass < ActiveRecord::SessionStore::Session

    def save
        ActiveRecord::Base.connected_to(role: :writing) do
            super
        end
    end

    def destroy
        ActiveRecord::Base.connected_to(role: :writing) do
            super
        end
    end
end

ActionDispatch::Session::ActiveRecordStore.session_class = ForceWriterSessionClass

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions