mirror of
https://github.com/TandoorRecipes/recipes.git
synced 2026-01-01 04:10:06 -05:00
added unique constraint on RecipeBookEntry table
Signed-off-by: Tobias Lindenberg <tobias@lindenberg.pm>
This commit is contained in:
31
cookbook/migrations/0096_auto_20210109_2044.py
Normal file
31
cookbook/migrations/0096_auto_20210109_2044.py
Normal file
@@ -0,0 +1,31 @@
|
||||
# Generated by Django 3.1.5 on 2021-01-09 19:44
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
def delete_duplicate_bookmarks(apps, schema_editor):
|
||||
"""
|
||||
In this migration, a unique constraint is set on the fields `recipe` and `book`.
|
||||
If there are already duplicate entries, the migration will fail.
|
||||
Therefore all duplicate entries are deleted beforehand.
|
||||
"""
|
||||
RecipeBookEntry = apps.get_model('cookbook', 'RecipeBookEntry')
|
||||
|
||||
for row in RecipeBookEntry.objects.all():
|
||||
if RecipeBookEntry.objects.filter(recipe=row.recipe, book=row.book).count() > 1:
|
||||
row.delete()
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
('cookbook', '0095_auto_20210107_1804'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
# run function to delete duplicated bookmarks
|
||||
migrations.RunPython(delete_duplicate_bookmarks),
|
||||
migrations.AlterUniqueTogether(
|
||||
name='recipebookentry',
|
||||
unique_together={('recipe', 'book')},
|
||||
),
|
||||
]
|
||||
Reference in New Issue
Block a user