[Python] i am facing django.db.migrations.exceptions.InvalidBasesError Cannot resolve bases,...

Discussão em 'Python' iniciado por Stack, Junho 20, 2025.

  1. Stack

    Stack Membro Participativo

    I have two apps, coreapp and product app. As all models in product app have common fields , i created a BaseModel in coreapp and then inherited the BaseModel in the models in product app. Then i ran python manage.py makemigrations and python manage.py migrate. After that i made some changes in the models, I added abstract = True in BaseModel and renamed some fields in the models in product app. Then i ran python manage.py makemigrations. Now, when i run python manage.py migrate i get the following error

    django.db.migrations.exceptions.InvalidBasesError: Cannot resolve bases for [<ModelState: 'product.Attribute'>, <ModelState: 'product.Category'>, <ModelState: 'product.Product'>, <ModelState: 'product.AttributeValue'>]
    This can happen if you are inheriting models from an app with migrations (e.g. contrib.auth)
    in an app with no migrations; see https://docs.djangoproject.com/en/4.1/topics/migrations/#dependencies for more


    Below are the two models

    coreapp/models.py

    from django.db import models

    # Create your models here.

    class BaseModel(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)


    product/models.py

    from django.db import models

    from project_root.coreapp.models import BaseModel
    # Create your models here.

    class Attribute(BaseModel):
    title = models.CharField(max_length=100)

    def __str__(self):
    return self.title

    class AttributeValue(BaseModel):
    title = models.CharField(max_length=100)
    attribute_object = models.ForeignKey(Attribute,on_delete=models.CASCADE, related_name='attribute_values')

    def __str__(self):
    return self.title

    class Category(BaseModel):
    title = models.CharField(max_length=200)
    parent= models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True)
    def __str__(self):
    return self.title

    class Product(BaseModel):
    title = models.CharField(max_length=500)
    price = models.FloatField()
    qty = models.BigIntegerField()
    sku = models.CharField(max_length=100)
    detail= models.TextField(max_length=5000)
    category_object = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='products')

    def __str__(self):
    return self.title


    When I ran python manage.py makemigrations first time i got the following migration files.

    coreapp/migrations/0001_initial.py

    from django.db import migrations, models


    class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
    migrations.CreateModel(
    name='BaseModel',
    fields=[
    ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
    ('created_at', models.DateTimeField(auto_now_add=True)),
    ('updated_at', models.DateTimeField(auto_now=True)),
    ],
    ),
    ]


    product/migrations/0001_initial.py

    # Generated by Django 4.1 on 2023-10-15 05:59

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


    class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ('coreapp', '0001_initial'),
    ]

    operations = [
    migrations.CreateModel(
    name='Attribute',
    fields=[
    ('basemodel_ptr', models.OneToOneField(auto_created=True,
    on_delete=django.db.models.deletion.CASCADE,
    parent_link=True, primary_key=True, serialize=False,
    to='coreapp.basemodel')),
    ('title', models.CharField(max_length=100)),
    ],
    bases=('coreapp.basemodel',),
    ),
    migrations.CreateModel(
    name='Category',
    fields=[
    ('basemodel_ptr', models.OneToOneField(auto_created=True,
    on_delete=django.db.models.deletion.CASCADE,
    parent_link=True, primary_key=True, serialize=False,
    to='coreapp.basemodel')),
    ('title', models.CharField(max_length=200)),
    ('parent', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE,
    to='product.category')),
    ],
    bases=('coreapp.basemodel',),
    ),
    migrations.CreateModel(
    name='Product',
    fields=[
    ('basemodel_ptr', models.OneToOneField(auto_created=True,
    on_delete=django.db.models.deletion.CASCADE,
    parent_link=True, primary_key=True, serialize=False,
    to='coreapp.basemodel')),
    ('title', models.CharField(max_length=500)),
    ('price', models.FloatField()),
    ('qty', models.BigIntegerField()),
    ('sku', models.CharField(max_length=100)),
    ('detail', models.TextField(max_length=5000)),
    ('category_object', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE,
    related_name='products', to='product.category')),
    ],
    bases=('coreapp.basemodel',),
    ),
    migrations.CreateModel(
    name='AttributeValue',
    fields=[
    ('basemodel_ptr', models.OneToOneField(auto_created=True,
    on_delete=django.db.models.deletion.CASCADE,
    parent_link=True, primary_key=True, serialize=False,
    to='coreapp.basemodel')),
    ('title', models.CharField(max_length=100)),
    ('attribute_object', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE,
    related_name='attribute_values', to='product.attribute')),
    ],
    bases=('coreapp.basemodel',),
    ),
    ]
    print('product migration 1')


    then I added abstract=True in BaseModel and renamed fieild attribute_object to attribute in AttributeValue model and category_object to category in Product model. After that i ran python manage.py makemigrations and got the following migrations files

    coreapp/migrations/0002_delete_basemodel.py

    # Generated by Django 4.1 on 2023-10-15 06:03

    from django.db import migrations


    class Migration(migrations.Migration):

    dependencies = [
    ('product', '0002_rename_attribute_object_attributevalue_attribute_and_more'),
    ('coreapp', '0001_initial'),
    ]

    operations = [
    migrations.DeleteModel(
    name='BaseModel',
    ),
    ]


    product/migrations/0002_rename_attribute_object_attributevalue_attribute_and_more.py

    # Generated by Django 4.1 on 2023-10-15 06:03

    from django.db import migrations, models
    import django.utils.timezone


    class Migration(migrations.Migration):

    dependencies = [
    ('product', '0001_initial'),
    ]

    operations = [
    migrations.RenameField(
    model_name='attributevalue',
    old_name='attribute_object',
    new_name='attribute',
    ),
    migrations.RenameField(
    model_name='product',
    old_name='category_object',
    new_name='category',
    ),
    migrations.RemoveField(
    model_name='attribute',
    name='basemodel_ptr',
    ),
    migrations.RemoveField(
    model_name='attributevalue',
    name='basemodel_ptr',
    ),
    migrations.RemoveField(
    model_name='category',
    name='basemodel_ptr',
    ),
    migrations.RemoveField(
    model_name='product',
    name='basemodel_ptr',
    ),
    migrations.AddField(
    model_name='attribute',
    name='created_at',
    field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
    preserve_default=False,
    ),
    migrations.AddField(
    model_name='attribute',
    name='id',
    field=models.BigAutoField(auto_created=True, default=1, primary_key=True, serialize=False,
    verbose_name='ID'),
    preserve_default=False,
    ),
    migrations.AddField(
    model_name='attribute',
    name='updated_at',
    field=models.DateTimeField(auto_now=True),
    ),
    migrations.AddField(
    model_name='attributevalue',
    name='created_at',
    field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
    preserve_default=False,
    ),
    migrations.AddField(
    model_name='attributevalue',
    name='id',
    field=models.BigAutoField(auto_created=True, default=1, primary_key=True, serialize=False,
    verbose_name='ID'),
    preserve_default=False,
    ),
    migrations.AddField(
    model_name='attributevalue',
    name='updated_at',
    field=models.DateTimeField(auto_now=True),
    ),
    migrations.AddField(
    model_name='category',
    name='created_at',
    field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
    preserve_default=False,
    ),
    migrations.AddField(
    model_name='category',
    name='id',
    field=models.BigAutoField(auto_created=True, default=1, primary_key=True, serialize=False,
    verbose_name='ID'),
    preserve_default=False,
    ),
    migrations.AddField(
    model_name='category',
    name='updated_at',
    field=models.DateTimeField(auto_now=True),
    ),
    migrations.AddField(
    model_name='product',
    name='created_at',
    field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
    preserve_default=False,
    ),
    migrations.AddField(
    model_name='product',
    name='id',
    field=models.BigAutoField(auto_created=True, default=1, primary_key=True, serialize=False,
    verbose_name='ID'),
    preserve_default=False,
    ),
    migrations.AddField(
    model_name='product',
    name='updated_at',
    field=models.DateTimeField(auto_now=True),
    ),
    ]

    print('product migration 2')


    Now when I run python manage.py migrate i get the error

    django.db.migrations.exceptions.InvalidBasesError: Cannot resolve bases for [<ModelState: 'product.Attribute'>, <ModelState: 'product.Category'>, <ModelState: 'product.Product'>, <ModelState: 'product.AttributeValue'>]
    This can happen if you are inheriting models from an app with migrations (e.g. contrib.auth)
    in an app with no migrations; see https://docs.djangoproject.com/en/4.1/topics/migrations/#dependencies for more


    I tried migrating the app separately in different order like

    python manage.py migrate product
    python manage.py migrate coreapp


    i also tried changing the order of dependencies in migration files

    but none worked.

    How do I resolve this issue?

    Continue reading...

Compartilhe esta Página