# site\_settings 的 name 列没有唯一索引

**URL:** <https://meta.discourse.org/t/no-unique-index-on-site-settings-name-column/112047>\
**Category:** Development\
**Created:** [2019 年3 月 19 日 14: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:** 1\
**Showing post:** 1

<div class="post-metadata">

作者： ![LeoMcA](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/leomca/32/87233_2.png) [@LeoMcA](https://meta.discourse.org/u/LeoMcA)\
发布日期： [2019 年3 月 19 日 14: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.

---

_[View the full topic](https://meta.discourse.org/t/no-unique-index-on-site-settings-name-column/112047)._
