# Discourse DB Error When Creating New Users via SSO Login

**URL:** https://meta.discourse.org/t/discourse-db-error-when-creating-new-users-via-sso-login/127442
**Category:** Bug
**Created:** [9월 2, 2019, 10:14오후 UTC](https://meta.discourse.org/t/discourse-db-error-when-creating-new-users-via-sso-login/127442 "2019-09-02T22:14:41Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Eingang](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eingang/32/98491_2.png) [@Eingang](https://meta.discourse.org/u/Eingang)
#### Post date: [9월 2, 2019, 10:14오후 UTC](https://meta.discourse.org/t/discourse-db-error-when-creating-new-users-via-sso-login/127442/1 "2019-09-02T22:14:41Z")

</div>

## Summary

Discourse DB constraint error because of duplicate key values generated when users without an account attempt to log in via SSO. Causes internal server error (500) to be seen via external proxy.

## Discussion

On August 28th or 29th, I updated Discourse to the latest beta (v2.4.0.beta2) along with all the installed Discourse plug-ins. Around the same time, I also updated [SeAT](https://github.com/eveseat/seat), and reconfigured Discourse to use a UNIX socket, which the external Apache can proxy/reverse proxy to. Prior to that, people were able to login via SeAT’s EvE Online SSO and new Discourse user accounts would be created for them if they didn’t already have one.

Since then, no new user accounts have successfully been created. Instead, the three people who have tried have generated `Failed to create or lookup user: ERROR: duplicate key value violates constraint "idx_category_users_user_id_category_id" DETAIL: Key (user_id, category_id)=(36, 6) already exists.` errors. The `category_id` is always `6` but the `user_id` varies between 33 and 44, even repeating numbers.

Assuming that `category_id` refers to entries in `categories`, then `6` seems to be `Grass Clippings`. Assuming that `user_id` refers to entries in `users`, then we don’t have any entries higher than `32`.

`Grass Clippings` as a category is restricted for at least reading to people in the `2GTHRDiscourse`, `2GTHRHighCouncil`, and `GRoLLake` groups. At least one of the people trying to connect had an `add_groups` command that included all three groups. The first person did not yet have one of the three, and the middle person had at least one of the three. They all generate the same kind of database error with the identical details except except the `user_id`.

People with Discourse accounts created by logging in via SSO prior to the upgrades do not seem to be experiencing problems logging in.

## Things Tried or Looked At

1. Turning off `force https` and reverting back to external Apache proxying to nginx ports.  
Unsurprisingly, this had no effect.

2. [Debugging and fixing common SSO issues](https://meta.discourse.org/t/debugging-and-fixing-common-sso-issues/103496)

3. [Watch out for double url encoding of SSO payload](https://meta.discourse.org/t/watch-out-for-double-url-encoding-of-sso-payload/124185)

4. Followed through the backtrace and could see Discourse made it to `models/discourse_single_sign_on.rb` at line 203 of `match_email_or_create_user()` where it makes a call to `User.create!()`. Alas, the logging of the params, etc. is _after_ that point, which it never gets to because it stops processing and returns a HTTP error 500. I checked `shared/log/rails/production.log.*` just in case.

## Other Comments

- SeAT: While the [SeAT Discourse plug-in](https://github.com/herpaderpaldent/seat-discourse) wasn’t updated, many components of SeAT were. It’s possible something in there has changed. However, the error is clearly on the Discourse side when trying to create or lookup a non-existent user, so I’m somewhat dubious the problem is on the SeAT side.

- Discourse: I can’t remember what beta version of Discourse I was running before the upgrade. I’d guess it had probably been at least a month or two since I’d last upgraded.

## Additional Details

> **Error**
>
> ```plaintext
> Failed to create or lookup user: ERROR: duplicate key value violates unique constraint "idx_category_users_user_id_category_id"
> DETAIL: Key (user_id, category_id)=(36, 6) already exists.
> . add_groups: CORPBLUEP,2GTHRAllianceMember,2GTHRALL,2GTHRHighCouncil,2GTHRCorpDirector,2GTHROfficer,DoWCitadelGunner,HCDIM,CORPDIRBLUEP,2GTHRCorpRecruiter,CORPDIRLAWNS,CORPDIR123HC,GRoLLake,DoWCapital,DoWFax,User,CORPHRBLUEP,2GTHRDiscourse
> admin: 
> moderator: 
> avatar_force_update: 
> avatar_url: http://image.eveonline.com/Character/740426190_128.jpg
> bio: 
> card_background_url: 
> email: something@somewhere.com
> external_id: 740426190
> groups: 
> locale: 
> locale_force_update: 
> name: Mindframe
> nonce: e6f9da9e62b720a1efd97348ca20f101
> profile_background_url: 
> remove_groups: Superuser,SPRKYAllianceMember,DoWFieldMarshall,DoWScoutCoordinator,DoWFCGang,DoWFCAlliance,DoWFCSenior,DoWScout,DoWSuper,DoWTitan,DoWFC,2GTHRCorpCEO,DoWTitanBridging,DoLLogisticsSov,DoLLogisticsShipping,HCDoW,HCDoL,HCDoC,HCDoF,DoCDoIT,DoCTechnoMage,DoCDiplomat,DoCDepartmentLead,DoCRecruiterPilots,DoCRecruiterCorporations,DoCDiplomatLead,DoWBLOP,ViewGroups,CORPAIEU,CORPDIRAIEU,CORPZBMC,CORPC.R.Y,CORP123HC,CORPALTBH,CORPLAWNS,CORPDIRZBMC,CORPDIRC.R.Y,CORPDIRALTBH,CORPF3R4L,CORPDIRF3R4L,CORPSPRKY,CORPDIRSPRKY,CORPRMKR,CORPDIRRMKR,CORPHR123HC,CORPHRAIEU,CORPHRZBMC,CORPHRC.R.Y,CORPHRF3R4L,SPRKYDiscourse,CORPHRLAWNS,CORPDIROUSYN,CORPHROUSYN,CORPOUSYN,DoWReimbursementTeam,DoWCarrier,DoWDread,WCMember,WCALL,WCMindframesMarketVisitors
> require_activation: 
> return_sso_url: 
> suppress_welcome_message: 
> title: 
> username: Mindframe
> website: /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-mini-profiler-1.0.2/lib/patches/db/pg.rb:110:in `async_exec'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-mini-profiler-1.0.2/lib/patches/db/pg.rb:110:in `async_exec'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/mini_sql-0.2.2/lib/mini_sql/postgres/connection.rb:118:in `run'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/mini_sql-0.2.2/lib/mini_sql/postgres/connection.rb:90:in `exec'
> /var/www/discourse/app/models/user.rb:1403:in `set_default_categories_preferences'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/callbacks.rb:426:in `block in make_lambda'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/callbacks.rb:236:in `block in halting_and_conditional'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/callbacks.rb:517:in `block in invoke_after'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/callbacks.rb:517:in `each'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/callbacks.rb:517:in `invoke_after'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/callbacks.rb:133:in `run_callbacks'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/callbacks.rb:816:in `_run_create_callbacks'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.3/lib/active_record/callbacks.rb:346:in `_create_record'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.3/lib/active_record/timestamp.rb:102:in `_create_record'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.3/lib/active_record/persistence.rb:705:in `create_or_update'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.3/lib/active_record/callbacks.rb:342:in `block in create_or_update'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/callbacks.rb:132:in `run_callbacks'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/callbacks.rb:816:in `_run_save_callbacks'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.3/lib/active_record/callbacks.rb:342:in `create_or_update'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.3/lib/active_record/persistence.rb:308:in `save!'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.3/lib/active_record/validations.rb:52:in `save!'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.3/lib/active_record/transactions.rb:315:in `block in save!'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.3/lib/active_record/transactions.rb:387:in `block in with_transaction_returning_status'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:267:in `block in transaction'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/transaction.rb:239:in `block in within_new_transaction'
> /usr/local/lib/ruby/2.6.0/monitor.rb:230:in `mon_synchronize'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/transaction.rb:236:in `within_new_transaction'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:267:in `transaction'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.3/lib/active_record/transactions.rb:212:in `transaction'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.3/lib/active_record/transactions.rb:385:in `with_transaction_returning_status'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.3/lib/active_record/transactions.rb:315:in `save!'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.3/lib/active_record/suppressor.rb:48:in `save!'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.3/lib/active_record/persistence.rb:53:in `create!'
> /var/www/discourse/app/models/discourse_single_sign_on.rb:203:in `block in match_email_or_create_user'
> /var/www/discourse/lib/distributed_mutex.rb:31:in `block in synchronize'
> /var/www/discourse/lib/distributed_mutex.rb:27:in `synchronize'
> /var/www/discourse/lib/distributed_mutex.rb:27:in `synchronize'
> /var/www/discourse/lib/distributed_mutex.rb:12:in `synchronize'
> /var/www/discourse/app/models/discourse_single_sign_on.rb:186:in `match_email_or_create_user'
> /var/www/discourse/app/models/discourse_single_sign_on.rb:74:in `lookup_or_create_user'
> /var/www/discourse/app/controllers/session_controller.rb:169:in `sso_login'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.3/lib/abstract_controller/base.rb:194:in `process_action'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_controller/metal/rendering.rb:30:in `process_action'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.3/lib/abstract_controller/callbacks.rb:42:in `block in process_action'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/callbacks.rb:132:in `run_callbacks'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.3/lib/abstract_controller/callbacks.rb:41:in `process_action'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_controller/metal/rescue.rb:22:in `process_action'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_controller/metal/instrumentation.rb:34:in `block in process_action'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/notifications.rb:168:in `block in instrument'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/notifications/instrumenter.rb:23:in `instrument'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/notifications.rb:168:in `instrument'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_controller/metal/instrumentation.rb:32:in `process_action'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_controller/metal/params_wrapper.rb:256:in `process_action'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.3/lib/active_record/railties/controller_runtime.rb:24:in `process_action'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.3/lib/abstract_controller/base.rb:134:in `process'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionview-5.2.3/lib/action_view/rendering.rb:32:in `process'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-mini-profiler-1.0.2/lib/mini_profiler/profiling_methods.rb:78:in `block in profile_method'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_controller/metal.rb:191:in `dispatch'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_controller/metal.rb:252:in `dispatch'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/routing/route_set.rb:52:in `dispatch'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/routing/route_set.rb:34:in `serve'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/journey/router.rb:52:in `block in serve'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/journey/router.rb:35:in `each'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/journey/router.rb:35:in `serve'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/routing/route_set.rb:840:in `call'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-protection-2.0.7/lib/rack/protection/frame_options.rb:31:in `call'
> /var/www/discourse/lib/middleware/omniauth_bypass_middleware.rb:68:in `call'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/tempfile_reaper.rb:15:in `call'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/conditional_get.rb:25:in `call'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/head.rb:12:in `call'
> /var/www/discourse/lib/content_security_policy/middleware.rb:12:in `call'
> /var/www/discourse/lib/middleware/anonymous_cache.rb:219:in `call'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/session/abstract/id.rb:232:in `context'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/session/abstract/id.rb:226:in `call'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/middleware/cookies.rb:670:in `call'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/callbacks.rb:98:in `run_callbacks'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/middleware/callbacks.rb:26:in `call'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/middleware/debug_exceptions.rb:61:in `call'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/logster-2.3.2/lib/logster/middleware/reporter.rb:43:in `call'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/railties-5.2.3/lib/rails/rack/logger.rb:38:in `call_app'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/railties-5.2.3/lib/rails/rack/logger.rb:28:in `call'
> /var/www/discourse/config/initializers/100-quiet_logger.rb:18:in `call'
> /var/www/discourse/config/initializers/100-silence_logger.rb:31:in `call'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/middleware/request_id.rb:27:in `call'
> /var/www/discourse/lib/middleware/enforce_hostname.rb:17:in `call'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/method_override.rb:22:in `call'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_dispatch/middleware/executor.rb:14:in `call'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/sendfile.rb:111:in `call'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-mini-profiler-1.0.2/lib/mini_profiler/profiler.rb:171:in `call'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/message_bus-2.2.2/lib/message_bus/rack/middleware.rb:57:in `call'
> /var/www/discourse/lib/middleware/request_tracker.rb:169:in `call'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/railties-5.2.3/lib/rails/engine.rb:524:in `call'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/railties-5.2.3/lib/rails/railtie.rb:190:in `public_send'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/railties-5.2.3/lib/rails/railtie.rb:190:in `method_missing'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/urlmap.rb:68:in `block in call'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/urlmap.rb:53:in `each'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/urlmap.rb:53:in `call'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/unicorn-5.5.1/lib/unicorn/http_server.rb:605:in `process_client'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/unicorn-5.5.1/lib/unicorn/http_server.rb:700:in `worker_loop'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/unicorn-5.5.1/lib/unicorn/http_server.rb:548:in `spawn_missing_workers'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/unicorn-5.5.1/lib/unicorn/http_server.rb:144:in `start'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/gems/unicorn-5.5.1/bin/unicorn:128:in `<top (required)>'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/bin/unicorn:23:in `load'
> /var/www/discourse/vendor/bundle/ruby/2.6.0/bin/unicorn:23:in `<main>'
> 
> ```

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [9월 3, 2019, 4:35오전 UTC](https://meta.discourse.org/t/discourse-db-error-when-creating-new-users-via-sso-login/127442/2 "2019-09-03T04:35:22Z")

</div>

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
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?

---

<div class="post-metadata">

### Author: ![Eingang](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eingang/32/98491_2.png) [@Eingang](https://meta.discourse.org/u/Eingang)
#### Post date: [9월 3, 2019, 5:58오후 UTC](https://meta.discourse.org/t/discourse-db-error-when-creating-new-users-via-sso-login/127442/3 "2019-09-03T17:58:34Z")

</div>

> [@sam](#):
>
> Do you have a group that is **both** default watched and default tracked in site settings.

I have categories that are tracked and categories that are watched, but there’s no overlap between them. They’re all top-level categories. However, I have an overlap between `default categories watching` and `default categories watching first post`. A category is present in both of those and that category coincides with the category ID of the category mentioned in my DB constraint error. Could that be part of the problem?

---

<div class="post-metadata">

### Author: ![Eingang](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eingang/32/98491_2.png) [@Eingang](https://meta.discourse.org/u/Eingang)
#### Post date: [9월 3, 2019, 6:11오후 UTC](https://meta.discourse.org/t/discourse-db-error-when-creating-new-users-via-sso-login/127442/4 "2019-09-03T18:11:33Z")

</div>

> [@Eingang](#):
>
> I have an overlap between `default categories watching` and `default categories watching first post` . A category is present in both of those and that category coincides with the category ID of the category mentioned in my DB constraint error.

Replying to myself: Removing the category from `default categories watching first post` so that it was only present in `default categories watching` solved the problem; someone without an account who previously could not log in can now do so.

---

<div class="post-metadata">

### Author: ![debryc](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/debryc/32/101652_2.png) [@debryc](https://meta.discourse.org/u/debryc)
#### Post date: [10월 5, 2021, 11:48오후 UTC](https://meta.discourse.org/t/discourse-db-error-when-creating-new-users-via-sso-login/127442/6 "2021-10-05T23:48:45Z")

</div>

방금 이 버그를 경험했습니다.

`default categories regular`에서 Recruitment Notes와 Partner Notes가 반복되어 사용자가 로그인할 수 없었습니다.

 ![Screenshot of Google Chrome (10-5-21, 7-37-24 PM)](https://global.discourse-cdn.com/meta/original/3X/6/7/67625e98ea3ec650ab0542f299d6d8eba227e297.png)

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [10월 6, 2021, 1:58오전 UTC](https://meta.discourse.org/t/discourse-db-error-when-creating-new-users-via-sso-login/127442/7 "2021-10-06T01:58:41Z")

</div>

설정을 저장할 때 중복을 방지하는 것은 매우 쉬워야 합니다. 다만, 과거 기록을 수정하는 부분은 확실하지 않습니다.

@vinothkannans 저장 시 이러한 기본 설정이 불필요한 데이터를 정리하도록 확인해 주시겠습니까?

- 중복 항목 제거
- A 카테고리에 대해 "기본 시청"을 저장하려고 하는데 이미 A에 "기본 추적"이 설정되어 있는 경우, 문제를 설명하면서 오류를 발생시킴

---

<div class="post-metadata">

### Author: ![debryc](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/debryc/32/101652_2.png) [@debryc](https://meta.discourse.org/u/debryc)
#### Post date: [10월 6, 2021, 3:31오후 UTC](https://meta.discourse.org/t/discourse-db-error-when-creating-new-users-via-sso-login/127442/9 "2021-10-06T15:31:50Z")

</div>

이 문제가 발생한 이유에 대해 내가 가장 유력하게 추정하는 시나리오는 다음과 같습니다.

1. Partner Notes를 자체 카테고리로 생성하고 기본 카테고리(Regular)로 설정했습니다.
2. Recruitment Notes를 자체 카테고리로 생성하고 기본 카테고리(Regular)로 설정했습니다.
3. Organizers를 자체 카테고리로 생성하고 기본 카테고리(Regular)로 설정했습니다.
4. Partner Notes와 Recruitment Notes의 설정을 변경하여 상위 카테고리를 Organizers로 지정했으며, (아마도) 기본 카테고리(Regular) 설정에는 아무런 변경을 가하지 않았습니다.

새로운 사용자가 가입할 수 없는 상황은 재앙적이며, 신규 사용자는 대개 그냥 포기하고 도움을 요청하지 않을 가능성이 높기 때문에 이를 포착하기도 어렵습니다. 따라서 이 버그를 수정하는 데 전적으로 동의합니다.

그동안, 오류 메시지에 "가입 시 문제가 계속 발생하면 `site email`로 연락하여 지원을 요청해 주세요."라는 문구를 포함할 수 있을 것입니다.
