Discourse DB Error When Creating New Users via SSO Login

This relates to this code:

https://github.com/discourse/discourse/blob/b9954b53bbaf65d3c5ff6eb9a1f321a61408e768/app/models/user.rb#L1389-L1405

What happened here is that we amended the index on category user like so:

https://github.com/discourse/discourse/blob/b9954b53bbaf65d3c5ff6eb9a1f321a61408e768/app/models/category_user.rb#L215-L216

Do you have a group that is both default watched and default tracked in site settings.

Look at: default categories watching default categories tracking default categories muted and default categories watching first post,

This fixes it:

diff --git a/app/models/user.rb b/app/models/user.rb
index c1a94949a6..85b2ca9244 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -1390,10 +1390,15 @@ class User < ActiveRecord::Base
     return if self.staged?
 
     values = []
+    # allocate set later
+    seen = nil
 
     %w{watching watching_first_post tracking muted}.each do |s|
       category_ids = SiteSetting.get("default_categories_#{s}").split("|").map(&:to_i)
       category_ids.each do |category_id|
+        seen ||= Set.new
+        next if seen.include?(category_id)
+        seen << category_id
         next if category_id == 0
         values << "(#{self.id}, #{category_id}, #{CategoryUser.notification_levels[s.to_sym]})"
       end
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index cc50d88b2e..4075ee6194 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -1603,8 +1603,12 @@ describe User do
 
       SiteSetting.default_categories_watching = category0.id.to_s
       SiteSetting.default_categories_tracking = category1.id.to_s
-      SiteSetting.default_categories_muted = category2.id.to_s
+
+      # this is invalid, but we don't validate so ensure nothing breaks
+      SiteSetting.default_categories_muted = "#{category2.id}|#{category0.id}"
+
       SiteSetting.default_categories_watching_first_post = category3.id.to_s
+
     end
 
     it "has overriden preferences" do

But I am not a fan of this fix, the site settings should validate that there is no overlap on save and we should migrate away the bad data.

@daniel I think you introduced the new constraint here, maybe followup with a validation when people set the site setting?

3 Likes