tsukasaoishi / fresh_connection

FreshConnection provides access to one or more configured database replicas.
MIT License
58 stars 11 forks source link

Validate uniqueness of causing reads to go to master #30

Closed ccastaneda13 closed 5 years ago

ccastaneda13 commented 5 years ago

Using fresh_connection 2.4.4 and when trying to create a record that has a uniqueness validation, the query checks for existence of the record against master and not replica. Is there any way to force the query to go to replica? Thanks

Running Rails 4.2.7 Ruby 2.3.4

tsukasaoishi commented 5 years ago

The validation query must be executed on master DB. Because of you can't validate when replication delay occurs.

example:

class User
  validates :email, uniqueness: true
end

User.create!(email: "tsukasa.oishi@gmail.com")
# If replication delay is occuring, the following query will succeed!
User.create!(email: "tsukasa.oishi@gmail.com")
ccastaneda13 commented 5 years ago

@tsukasaoishi thanks for the response! I see, will move the constraint to the database and remove the AR validation to avoid this query going to master.