-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path0019_auto_20160930_2351.py
More file actions
40 lines (32 loc) · 1.06 KB
/
0019_auto_20160930_2351.py
File metadata and controls
40 lines (32 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2016-09-30 23:51
from __future__ import unicode_literals
from django.db import migrations, models
from django.db.models import Count
def fix_duplicate_keys(apps, schema_editor):
Keys = apps.get_model("api", "Key")
# find duplicates
duplicates = Keys.objects.values('id') \
.annotate(Count('id')) \
.order_by() \
.filter(id__count__gt=1)
for dup in duplicates:
# update all duplicates
inc = 1
for key in Keys.objects.filter(id=dup['id']):
key_id = '{}-{}'.format(key.id, inc)
key.id = key_id
key.save()
inc += 1
class Migration(migrations.Migration):
dependencies = [
('api', '0018_auto_20160908_1748'),
]
operations = [
migrations.RunPython(fix_duplicate_keys),
migrations.AlterField(
model_name='key',
name='id',
field=models.CharField(max_length=128, unique=True),
),
]