Skip to content

[#74977] Modify friendly_id_slugs constraints.#23223

Open
ba1ash wants to merge 1 commit into
devfrom
code-maintenance/74977-treat-null-scope-as-uniq-value-in-index_friendly_id_slugs_on_slug_and_sluggable_type_and_scope
Open

[#74977] Modify friendly_id_slugs constraints.#23223
ba1ash wants to merge 1 commit into
devfrom
code-maintenance/74977-treat-null-scope-as-uniq-value-in-index_friendly_id_slugs_on_slug_and_sluggable_type_and_scope

Conversation

@ba1ash
Copy link
Copy Markdown
Member

@ba1ash ba1ash commented May 15, 2026

https://community.openproject.org/wp/74977

Add NOT NULL to sluggable_type and fix NULL scope uniqueness in friendly_id_slugs

  • Delete duplicate NULL scope rows, keeping the most recent per (slug, sluggable_type)
  • Delete NULL sluggable_type rows before adding NOT NULL constraint
  • Replace unique index on (slug, sluggable_type, scope) with NULLS NOT DISTINCT variant (requires Postgres 15+)

Why?

  1. sluggable_type NOT NULL
    Every slug must belong to a model (e.g. Post, User). A slug with no sluggable_type is orphaned and meaningless.
  2. NULLS NOT DISTINCT
    In the unique constraint not treating every NULL scope as unique leads to duplicate records if scope is NULL which does not make sense and pollutes the table with unnecessary data

@ba1ash ba1ash self-assigned this May 15, 2026
@ba1ash ba1ash requested review from akabiru and as-op May 15, 2026 11:47
https://community.openproject.org/wp/74977

Add NOT NULL to *sluggable_type* and fix NULL *scope* uniqueness in *friendly_id_slugs*

- Delete duplicate NULL *scope* rows, keeping the most recent per (slug, sluggable_type)
- Delete NULL *sluggable_type* rows before adding NOT NULL constraint
- Replace unique index on (slug, sluggable_type, scope) with NULLS NOT DISTINCT variant (requires Postgres 15+)

Why?

1. *sluggable_type* NOT NULL
  every slug must belong to a model (e.g. Post, User). A slug with no *sluggable_type* is orphaned and meaningless.
2. NULLS NOT DISTINCT in the unique constraint
  not treating every NULL *scope* as unique leads to duplicate records if *scope* is NULL which does not make sense
  and pollutes the table with unnecessary data
@ba1ash ba1ash force-pushed the code-maintenance/74977-treat-null-scope-as-uniq-value-in-index_friendly_id_slugs_on_slug_and_sluggable_type_and_scope branch from 2a1aca4 to 357b476 Compare May 15, 2026 11:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant