From a42eda12dab157b1947f6d423b8d687dad98efe5 Mon Sep 17 00:00:00 2001 From: Przemek Sobstel Date: Mon, 12 Feb 2024 16:22:29 +0100 Subject: [PATCH] doc: Warn against having uniqueness validator when create_or_find_by is used Co-authored-by: Petrik de Heus --- activerecord/lib/active_record/relation.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index f1c9f6e0d43..7af8e108dcc 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -255,6 +255,8 @@ module ActiveRecord # the problem of running out of integers, if the underlying table is still stuck on a primary # key of type int (note: All \Rails apps since 5.1+ have defaulted to bigint, which is not liable # to this problem). + # * Columns with unique database constraints should not have uniqueness validations defined, + # otherwise #create will fail due to validation errors and #find_by will never be called. # # This method will return a record if all given attributes are covered by unique constraints # (unless the INSERT -> DELETE -> SELECT race condition is triggered), but if creation was attempted