FIX: Add unique index to prevent duplicate slugs for categories

FIX: Add unique index to prevent duplicate slugs for categories

diff --git a/app/models/category.rb b/app/models/category.rb
index a903c8f366..db500e6ff6 100644
--- a/app/models/category.rb
+++ b/app/models/category.rb
@@ -890,4 +890,5 @@ end
 #  index_categories_on_search_priority         (search_priority)
 #  index_categories_on_topic_count             (topic_count)
 #  unique_index_categories_on_name             (COALESCE(parent_category_id, '-1'::integer), name) UNIQUE
+#  unique_index_categories_on_slug             (COALESCE(parent_category_id, '-1'::integer), slug) UNIQUE
 #
diff --git a/db/migrate/20191008124357_add_unique_index_categories_on_slug.rb b/db/migrate/20191008124357_add_unique_index_categories_on_slug.rb
new file mode 100644
index 0000000000..5ee97c6482
--- /dev/null
+++ b/db/migrate/20191008124357_add_unique_index_categories_on_slug.rb
@@ -0,0 +1,12 @@
+# frozen_string_literal: true
+
+class AddUniqueIndexCategoriesOnSlug < ActiveRecord::Migration[6.0]
+  def change
+    add_index(
+      :categories,
+      'COALESCE(parent_category_id, -1), slug',
+      name: 'unique_index_categories_on_slug',
+      unique: true
+    )
+  end
+end

GitHub sha: c71da3f3

1 Like

This commit has been mentioned on Discourse Meta. There might be relevant details there: