mitty1293 / myorders

Orders history
MIT License
0 stars 0 forks source link

Unitモデルより先にItemモデルが定義されてしまう #4

Closed mitty1293 closed 2 years ago

mitty1293 commented 2 years ago
class Unit(models.Model):
    class Meta:
        db_table = "unit"

    name = models.CharField(
        db_column="name",
        verbose_name="単位",
        max_length=32,
    )

    def __str__(self):
        return self.name

class Item(models.Model):
    class Meta:
        db_table = "item"

    unit = models.ForeignKey(
        Unit,
        db_column="unit",
        verbose_name="単位",
        on_delete=models.SET_DEFAULT,
        default=get_or_create_undefined_unit,
    )

上記のように定義しているのだが、以下のようになる。

# poetry run python manage.py makemigrations
Migrations for 'order_history':
  order_history/migrations/0001_initial.py
    - Create model Category
    - Create model Item
    - Create model Unit
    - Create model Vendor
    - Create model OrderHistory
    - Add field unit to item

Item内fieldでUnitを使っているので先にUnitを定義してほしいのだけど。。。


    operations = [
        migrations.CreateModel(
            name='Category',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(db_column='name', max_length=64, verbose_name='カテゴリ名')),
            ],
            options={
                'db_table': 'category',
            },
        ),
        migrations.CreateModel(
            name='Item',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(db_column='name', max_length=64, verbose_name='商品名')),
                ('manufacturer', models.CharField(blank=True, db_column='manufacturer', max_length=64, verbose_name='製造者')),
                ('producing_area', models.CharField(blank=True, db_column='producing_area', max_length=64, verbose_name='生産地')),
                ('category', models.ForeignKey(db_column='category', default=order_history.models.get_or_create_undefined_category, on_delete=django.db.models.deletion.SET_DEFAULT, to='order_history.category', verbose_name='カテゴリ')),
            ],
            options={
                'db_table': 'item',
            },
        ),
        migrations.CreateModel(
            name='Unit',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(db_column='name', max_length=32, verbose_name='単位')),
            ],
            options={
                'db_table': 'unit',
            },
        ),

        migrations.AddField(
                    model_name='item',
                    name='unit',
                    field=models.ForeignKey(db_column='unit', default=order_history.models.get_or_create_undefined_unit, on_delete=django.db.models.deletion.SET_DEFAULT, to='order_history.unit', verbose_name='単位'),
        ),
mitty1293 commented 2 years ago

https://code.djangoproject.com/ticket/27768 https://stackoverflow.com/questions/69927879/unnecessary-auto-migrations-on-django

mitty1293 commented 2 years ago

class Itemclass Productに変更したらうまくいった。 何やら名前の付け方によってそうした状況になることがあるらしい。