# Generated by Django 5.1.4 on 2025-05-01 09:51

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


def set_scale(apps, schema_editor):
    Consist = apps.get_model("consist", "Consist")

    for consist in Consist.objects.all():
        try:
            consist.scale = consist.consist_item.first().rolling_stock.scale
            consist.save()
        except AttributeError:
            pass


class Migration(migrations.Migration):

    dependencies = [
        ("consist", "0016_alter_consistitem_order"),
        (
            "metadata",
            "0024_remove_genericdocument_tags_delete_decoderdocument_and_more",
        ),
    ]

    operations = [
        migrations.AddField(
            model_name="consist",
            name="scale",
            field=models.ForeignKey(
                null=True,
                on_delete=django.db.models.deletion.CASCADE,
                to="metadata.scale",
            ),
        ),
        migrations.RunPython(
            set_scale,
            reverse_code=migrations.RunPython.noop
        ),
    ]
