# Generated by Django 5.2.4 on 2026-09-02 19:01

import django.db.models.deletion
from django.db import migrations, models


def backfill_student_group_links(apps, schema_editor):
    EsStudent = apps.get_model("api", "EsStudent")
    EsStudentStudentGroup = apps.get_model("api", "EsStudentStudentGroup")
    rows = [
        EsStudentStudentGroup(student_id=student.id, student_group_id=student.student_group_id)
        for student in EsStudent.objects.filter(student_group_id__isnull=False).iterator()
    ]
    if rows:
        EsStudentStudentGroup.objects.bulk_create(rows, batch_size=500, ignore_conflicts=True)


class Migration(migrations.Migration):

    dependencies = [
        ('api', '0068_esstudent_tt_id'),
    ]

    operations = [
        migrations.CreateModel(
            name='EsStudentStudentGroup',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('student', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='student_group_links', to='api.esstudent')),
                ('student_group', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='student_links', to='api.esstudentgroup')),
            ],
            options={
                'db_table': 'es_student_student_group',
                'unique_together': {('student', 'student_group')},
            },
        ),
        migrations.RunPython(backfill_student_group_links, migrations.RunPython.noop),
    ]
