# No unique index on site\_settings name column

**URL:** https://meta.discourse.org/t/no-unique-index-on-site-settings-name-column/112047
**Category:** Development
**Created:** [3월 19, 2019, 2:03오후 UTC](https://meta.discourse.org/t/no-unique-index-on-site-settings-name-column/112047 "2019-03-19T14:03:47Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![LeoMcA](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/leomca/32/87233_2.png) [@LeoMcA](https://meta.discourse.org/u/LeoMcA)
#### Post date: [3월 19, 2019, 2:03오후 UTC](https://meta.discourse.org/t/no-unique-index-on-site-settings-name-column/112047/1 "2019-03-19T14:03:48Z")

</div>

When updating today, [this migration](https://github.com/discourse/discourse/blob/3fd04df781f1cb736228e3212ef79ab2ee20326e/db/migrate/20190227210035_add_missing_topic_id_site_settings.rb) failed:

```
PG::CardinalityViolation: ERROR: more than one row returned by a subquery used as an expression
: INSERT INTO site_settings(name, data_type, value, created_at, updated_at)
SELECT 'lounge_welcome_topic_id', 3, id, created_at, updated_at
FROM topics
WHERE title = 'Welcome to the Lounge' AND NOT EXISTS(
    SELECT 1
    FROM site_settings
    WHERE name = 'lounge_welcome_topic_id'
  ) AND category_id = (
  SELECT value::INT
  FROM site_settings
  WHERE name = 'lounge_category_id'
)
ORDER BY created_at
LIMIT 1

```

So I run the two subqueries, and… 🤨

```
SELECT *         
  FROM site_settings
  WHERE name = 'lounge_category_id';
 id | name | data_type | value | created_at | updated_at         
----+--------------------+-----------+-------+----------------------------+----------------------------
 60 | lounge_category_id | 3 | 14 | 2014-02-17 23:45:49.295261 | 2014-02-17 23:45:49.295261
  2 | lounge_category_id | 3 | 2 | 2014-02-17 23:45:39.227589 | 2014-02-18 02:31:03.144083
(2 rows)

```

That second row points to the actual lounge category (`id=2`), there’s no category with `id=14`.

I have no idea how we got duplicate `lounge_category_id` settings way back in 2014, and if this is the result of a _really_ legacy bug, or something weird that happened on our instance back then.

This is the only duplicated site setting I can see, but it highlights there’s no unique index on the `name` column, which I imagine there should be.

I’ve deleted the wrong row, and the migration ran fine.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [3월 20, 2019, 2:48오전 UTC](https://meta.discourse.org/t/no-unique-index-on-site-settings-name-column/112047/2 "2019-03-20T02:48:36Z")

</div>

I think this might be relevant to @gerhard

---

<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: [3월 20, 2019, 4:27오전 UTC](https://meta.discourse.org/t/no-unique-index-on-site-settings-name-column/112047/3 "2019-03-20T04:27:58Z")

</div>

Definitely relevant to @gerhard but strongly agree we should have a unique index on name on the `site_settings` table (and throw away non unique rows in the migration)

We will make that happen.

---

<div class="post-metadata">

### Author: ![gerhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/gerhard/32/119479_2.png) [@gerhard](https://meta.discourse.org/u/gerhard)
#### Post date: [3월 25, 2019, 4:33오후 UTC](https://meta.discourse.org/t/no-unique-index-on-site-settings-name-column/112047/5 "2019-03-25T16:33:02Z")

</div>

Done in [FIX: Make migration resilient against duplicate site settings · discourse/discourse@467c17d · GitHub](https://github.com/discourse/discourse/commit/467c17da53a9fe3c1f83c8c0321fd822ca5833fd) and [FIX: Remove duplicate site settings and add unique index · discourse/discourse@cb70a32 · GitHub](https://github.com/discourse/discourse/commit/cb70a328f51a25cd278df8f71d88f45df007740a)
