# Generated by Django 3.1.1 on 2020-09-22 11:49

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


def set_default_variants(apps, schema_editor):
    Product = apps.get_model("product", "Product")
    for product in Product.objects.iterator():
        first_variant = product.variants.first()
        if first_variant:
            product.default_variant = first_variant
            product.save(update_fields=["default_variant", "updated_at"])


class Migration(migrations.Migration):

    dependencies = [
        ("product", "0125_auto_20200916_1511"),
    ]

    operations = [
        migrations.AddField(
            model_name="product",
            name="default_variant",
            field=models.OneToOneField(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                related_name="+",
                to="product.productvariant",
            ),
        ),
        migrations.RunPython(
            set_default_variants, reverse_code=migrations.RunPython.noop
        ),
    ]
