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

ActiveRecord::ReadOnlyError when writing to user record on sign in #5757

Open
clarkjim2 opened this issue Jan 15, 2025 · 0 comments
Open

ActiveRecord::ReadOnlyError when writing to user record on sign in #5757

clarkjim2 opened this issue Jan 15, 2025 · 0 comments

Comments

@clarkjim2
Copy link

clarkjim2 commented Jan 15, 2025

Moving a new Rails 8.0.1 app from development to production. When testing the authentication for the first time in production, Devise cannot write to the user table when signing in a user:

Information for cause: ActiveRecord::ReadOnlyError (Write query attempted while in readonly mode: UPDATE "users" SET "sign_in_count" = $1, "current_sign_in_at" = $2, "last_sign_in_at" = $3, "current_sign_in_ip" = $4, "last_sign_in_ip" = $5, "updated_at" = $6 WHERE "users"."id" = $7)

The problem occurs in the session controller on this line:
sign_in(resource_name, resource)

To test, I can create and save a user record in the rails console. Until debugging this, I never knew of all the various options concerning primary and replica databases. I have only a primary Postgresql database (and others for cache, queue, and cable) and have added this to the application.rb file:

    config.active_record.writing_role = :primary
    config.active_record.reading_role = :primary
    
    config.after_initialize do
      ActiveRecord::Base.connected_to(role: :writing) do
        ActiveRecord::Base.connection.execute("SET SESSION CHARACTERISTICS AS TRANSACTION READ WRITE")
        ActiveRecord::Base.connection.execute("SET default_transaction_read_only = OFF")
      end
    end

And this is the updated database.yml section for the production database:

production: &production
  primary: &primary_production
    <<: *default
    database: app_production
    username: <%= ENV["POSTGRES_USER"] %>
    password: <%= ENV["POSTGRES_PASSWORD"] %>
    replica: false
    primary: true
    read_only: false
    variables:
      statement_timeout: 5000
      lock_timeout: 5000
      idle_in_transaction_session_timeout: 5000
      default_transaction_read_only: 'off'

Still no joy though and the user record continues to be blocked from writing in Devise. What do I need to do in production to allow it to be written to? Thanks!

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

No branches or pull requests

1 participant