Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jamienourish opened this issue May 26, 2021 · 1 comment
Open

Comments

@jamienourish
Copy link

jamienourish commented May 26, 2021

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
@vadimeremeev
Copy link

It still make sence with rails 6.1.4.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants