Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django model AutoField error

I had a model like this

class Alrule(models.Model):
    priority = models.PositiveIntegerField(unique=True)
    rule = models.CharField(max_length=256)

I migrated this model. After I decided to make priority field as primary key with AutoField

Then my model looked like this:

class Alrule(models.Model):
    priority = models.AutoField(primary_key=True)
    rule = models.CharField(max_length=256)

I made a migration and migrated this change.

Now again i needed to revert back. Now I made following changes:

class Alrule(models.Model):
    priority = models.PositiveIntegerField(unique=True)
    rule = models.CharField(max_length=256)

I made a migration. Now when I migrate I get the following ERROR:

AssertionError: A model can't have more than one AutoField.

My understanding is priority field was set to AutoField before and it is not removed. Please help

My first migration look like this:

operations = [
    migrations.RemoveField(
        model_name='alrule',
        name='id',
    ),
    migrations.AlterField(
        model_name='alrule',
        name='priority',
        field=models.AutoField(serialize=False, primary_key=True),
    ),
]

Second migration looks like this:

operations = [
    migrations.AddField(
        model_name='alrule',
        name='id',
        field=models.AutoField(auto_created=True, primary_key=True, default=2, serialize=False, verbose_name='ID'),
        preserve_default=False,
    ),
    migrations.AlterField(
        model_name='alrule',
        name='priority',
        field=models.PositiveIntegerField(unique=True),
    ),
]
like image 504
wrufesh Avatar asked Nov 20 '25 18:11

wrufesh


1 Answers

If you use mssql-django, there is some limitations:

The following features are currently not supported:

mssql-django does not support SQL-based regex commands

Altering a model field from or to AutoField at migration

https://github.com/microsoft/mssql-django#limitations

like image 117
Mahyar Majidi Avatar answered Nov 22 '25 18:11

Mahyar Majidi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!