Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IntegrityError caused by duplicate periodic task creation #3451

Merged
merged 1 commit into from
Oct 17, 2024

Conversation

Guanchishan
Copy link
Contributor

@Guanchishan Guanchishan commented Oct 11, 2024

Description

Summary
This PR addresses an issue with the creation of periodic tasks in the settings-dashboard view, where using PeriodicTask.objects.get_or_create() could lead to an IntegrityError due to duplicate primary keys when the task already exists.

Technical Details
The change involves replacing PeriodicTask.objects.get_or_create() with PeriodicTask.objects.update_or_create(). This modification ensures that if a periodic task with the specified name already exists, it will be updated instead of creating a new entry, thus preventing the IntegrityError that occurs due to a unique constraint violation on the primary key.

Changes Made

  • Updated PeriodicTask.objects.get_or_create() to PeriodicTask.objects.update_or_create() to prevent duplicate key errors when the task already exists.

Testing and Impact
This change has been tested to verify:

  • If the task with the given name already exists, the task is updated without creating duplicate entries.
  • If the task does not exist, a new entry is created correctly.

This update helps to prevent issues when multiple attempts are made to create a periodic task, especially in production environments, where such collisions can cause instability.

Additional Context
No other changes were made beyond this single line, to minimize potential side effects.

  • Related Issue #
  • Closes #

What type of Pull Request is this?

  • Bug Fix
  • Enhancement
  • Plumbing / Internals / Dependencies
  • Refactor

Does this PR change settings or dependencies, or break something?

  • This PR changes or adds default settings, configuration, or .env values
  • This PR changes or adds dependencies
  • This PR introduces other breaking changes

Details of breaking or configuration changes (if any of above checked)

Documentation

  • New or amended documentation will be required if this PR is merged
  • I have created a matching pull request in the Documentation repository
  • I intend to create a matching pull request in the Documentation repository after this PR is merged

Tests

  • My changes do not need new tests
  • All tests I have added are passing
  • I have written tests but need help to make them pass
  • I have not written tests and need help to write them

Change PeriodicTask.objects.get_or_create() to PeriodicTask.objects.update_or_create().

This change prevents a potential IntegrityError when creating a periodic task due to duplicate primary key. By using update_or_create, if the record already exists, it will be updated instead of attempting to insert a new record with the same primary key, ensuring the process completes without error.
@mouse-reeve
Copy link
Member

Thank you so much for all these bug fixes! The nuance here totally escaped me :)

@mouse-reeve mouse-reeve merged commit 7ecf3b6 into bookwyrm-social:main Oct 17, 2024
10 checks passed
@Guanchishan Guanchishan deleted the patch-1 branch October 18, 2024 03:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants