Fix IntegrityError caused by duplicate periodic task creation #3451
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Summary
This PR addresses an issue with the creation of periodic tasks in the
settings-dashboard
view, where usingPeriodicTask.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()
withPeriodicTask.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
PeriodicTask.objects.get_or_create()
toPeriodicTask.objects.update_or_create()
to prevent duplicate key errors when the task already exists.Testing and Impact
This change has been tested to verify:
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.
What type of Pull Request is this?
Does this PR change settings or dependencies, or break something?
Details of breaking or configuration changes (if any of above checked)
Documentation
Tests