Skip to content

fix: set default value for pinned field on CommentThread to prevent NULL sort bug#270

Merged
taimoor-ahmed-1 merged 5 commits intoopenedx:masterfrom
mitodl:anas/fix-pinned-field-default-in-comment-thread
Mar 30, 2026
Merged

fix: set default value for pinned field on CommentThread to prevent NULL sort bug#270
taimoor-ahmed-1 merged 5 commits intoopenedx:masterfrom
mitodl:anas/fix-pinned-field-default-in-comment-thread

Conversation

@Anas12091101
Copy link
Copy Markdown
Contributor

@Anas12091101 Anas12091101 commented Mar 27, 2026

Description

This PR sets the default value for the pinned field on CommentThread to prevent the NULL sort bug.

When a new CommentThread is created, the pinned field is set to None (NULL in the database). However, when a thread is pinned and then unpinned, pinned becomes False. This creates an inconsistency: some threads have pinned=None while others have pinned=False, even though both represent "not pinned."

This causes a sorting bug. Thread listing uses the sort order [-pinned, -last_activity_at], and in MySQL, False (0) sorts higher than NULL in descending order. As a result, previously-pinned-then-unpinned threads (pinned=False) appear above never-pinned threads (pinned=None), breaking the expected sort by last_activity_at.

How to test

  • Create a new discussion thread via the Discussions MFE or API.
  • Verify the thread's pinned value in the database is False, not None. You can check this in the LMS Django admin at /admin/forum/commentthread/.
  • Pin the thread, then unpin it. Confirm pinned is still False.
  • Create a second thread (do not pin it).
  • Set the sort order to "Recent activity" in the Discussions MFE.
  • Verify both threads sort correctly by last_activity_at, with neither thread incorrectly jumping above the other due to a pinned value mismatch.

Merge checklist:
Check off if complete or not applicable:

  • Version bumped
  • Changelog record added
  • Documentation updated (not only docstrings)
  • Fixup commits are squashed away
  • Unit tests added/updated
  • Manual testing instructions provided
  • Noted any: Concerns, dependencies, migration issues, deadlines, tickets

@openedx-webhooks
Copy link
Copy Markdown

Thanks for the pull request, @Anas12091101!

This repository is currently maintained by @openedx/wg-maintainers-forums.

Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review.

🔘 Get product approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.
🔘 Provide context

To help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads
🔘 Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.

Details
Where can I find more information?

If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources:

When can I expect my changes to be merged?

Our goal is to get community contributions seen and reviewed as efficiently as possible.

However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 As a result it may take up to several weeks or months to complete a review and merge your PR.

@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Mar 27, 2026
@github-project-automation github-project-automation bot moved this to Needs Triage in Contributions Mar 27, 2026
@Anas12091101 Anas12091101 force-pushed the anas/fix-pinned-field-default-in-comment-thread branch from 410f882 to 2db7a64 Compare March 27, 2026 09:47
@Anas12091101
Copy link
Copy Markdown
Contributor Author

@openedx/wg-maintainers-forums, could someone from the team please take a look at this PR when available?

@Anas12091101 Anas12091101 moved this from Needs Triage to Ready for Review in Contributions Mar 27, 2026
@taimoor-ahmed-1
Copy link
Copy Markdown
Contributor

Appreciate the PR @Anas12091101! - Just some thoughts on the migration:

This change makes pinned non-nullable with a default, but the migration does not handle existing NULL values.

If any existing CommentThread.pinned rows are NULL, this may cause issues during migration since setting a default only applies to new rows.

Suggested approach:

  1. Add a data migration to backfill existing NULL values to False
  2. Then alter the field to be non-nullable with default=False

Example:

def backfill_pinned(apps, schema_editor):
    CommentThread = apps.get_model("forum", "CommentThread")
    CommentThread.objects.filter(pinned__isnull=True).update(pinned=False)

operations = [
    migrations.RunPython(backfill_pinned, migrations.RunPython.noop),
    migrations.AlterField(
        model_name="commentthread",
        name="pinned",
        field=models.BooleanField(default=False),
    ),
]

@Anas12091101
Copy link
Copy Markdown
Contributor Author

@taimoor-ahmed-1, thanks for the review. I overlooked this because edX’s default DB provider is MySQL, which automatically converts NULL values to the default during migrations. I’ve now added a backfill function in the migration to ensure compatibility with other databases as well.

@taimoor-ahmed-1 taimoor-ahmed-1 merged commit 78b36e4 into openedx:master Mar 30, 2026
14 checks passed
@github-project-automation github-project-automation bot moved this from Ready for Review to Done in Contributions Mar 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

open-source-contribution PR author is not from Axim or 2U

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants