# Generated by Django 3.1.2 on 2020-11-15 06:25

from django.db import migrations, models
from django.apps.registry import Apps


def update_person_id_in_confessions(apps: Apps, schema_editor):
    Person = apps.get_model('index', 'Person')
    Confession = apps.get_model('index', 'Confession')
    for obj in Confession.objects.all():
        num_id = Person.objects.get(nickname=obj.sender).id
        obj.sender = num_id
        num_id = Person.objects.get(nickname=obj.receiver).id
        obj.receiver = num_id
        obj.save()


class Migration(migrations.Migration):

    dependencies = [
        ('index', '0004_auto_20201109_0036'),
    ]

    operations = [
        # revoke foreign key constaints
        migrations.AlterField(
            model_name='confession',
            name='sender',
            field=models.CharField(max_length=100, null=True),
        ),
        migrations.AlterField(
            model_name='confession',
            name='receiver',
            field=models.CharField(max_length=100, null=True),
        ),
        # update primary key
        migrations.AlterField(
            model_name='person',
            name='nickname',
            field=models.CharField(max_length=50),
        ),
        migrations.AlterField(
            model_name='person',
            name='id',
            field=models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')
        ),
        migrations.RunPython(update_person_id_in_confessions),
        # add foreign key constraints
        migrations.AlterField(
            model_name='confession',
            name='sender',
            field=models.ForeignKey(on_delete=models.CASCADE, related_name='sender_id', to='index.person'),
        ),
        migrations.AlterField(
            model_name='confession',
            name='receiver',
            field=models.ForeignKey(on_delete=models.CASCADE, related_name='sender_id', to='index.person'),
        ),
    ]