Excerpt
Steps to reproduce
I create a repo to reproduce a issue.
https://github.com/sinsoku/rails_issue_readonly_error
The outline of the repo is as follows.
Create an app with rails new
Create a model with rails g model user name
Configure for multiple databases to use replica(sinsoku/rails_issue_readonly_error@230a0fd)
Run the test after disabling fixtures(sinsoku/rails_issue_readonly_error@8421248)
Here is the test code I put in the repository.
test "should not raise ActiveRecord::ReadOnlyError" do
ActiveRecord::Base.connected_to(role: :reading) do
User.first
assert_nothing_raised do
ActiveRecord::Base.connected_to(role: :writing) { User.create! }
end
end
end
The test fails with ActiveRecord::ReadOnlyError.
$ bin/rails test
Running via Spring preloader in process 22117
Run options: --seed 65401
# Running:
E
Error:
UserTest#test_should_not_raise_ActiveRecord::ReadOnlyError:
ActiveRecord::ReadOnlyError: Write query attempted while in readonly mode: INSERT INTO "us
Steps to reproduce
I create a repo to reproduce a issue.
https://github.com/sinsoku/rails_issue_readonly_error
The outline of the repo is as follows.
Create an app with rails new
Create a model with rails g model user name
Configure for multiple databases to use replica(sinsoku/rails_issue_readonly_error@230a0fd)
Run the test after disabling fixtures(sinsoku/rails_issue_readonly_error@8421248)
Here is the test code I put in the repository.
test "should not raise ActiveRecord::ReadOnlyError" do
ActiveRecord::Base.connected_to(role: :reading) do
User.first
assert_nothing_raised do
ActiveRecord::Base.connected_to(role: :writing) { User.create! }
end
end
end
The test fails with ActiveRecord::ReadOnlyError.
$ bin/rails test
Running via Spring preloader in process 22117
Run options: --seed 65401
# Running:
E
Error:
UserTest#test_should_not_raise_ActiveRecord::ReadOnlyError:
ActiveRecord::ReadOnlyError: Write query attempted while in readonly mode: INSERT INTO "users" ("created_at", "updated_at") VALUES (?, ?)
test/models/user_test.rb:9:in `block (4 levels) in <class:UserTest>'
test/models/user_test.rb:9:in `block (3 levels) in <class:UserTest>'
test/models/user_test.rb:8:in `block (2 levels) in <class:UserTest>'
test/models/user_test.rb:5:in `block in <class:UserTest>'
rails test test/models/user_test.rb:4
Finished in 0.149822s, 6.6746 runs/s, 0.0000 assertions/s.
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
Use case
Use ActiveRecord::Middleware::DatabaseSelector
Create a record when a request is GET
Use factory_bot instead of fixtures
I found the issue under these conditions.
Expected behavior
ActiveRecord::ReadOnlyError should not raise.
Actual behavior
ActiveRecord::ReadOnlyError raises.
System configuration
Rails version: 6.0.3
Ruby version: 2.7.1
Related issues
#37765