Default Error for Django AutoField

Django 1.7.1, MySQL 5.6, Python 2.7.8

I had a model that looked like this:

class Host(models.Model):
    hostName = models.CharField(max_length=45, primary_key=True)
    ...

I deleted it primary_key=Truemanually, which made it manage.py sqlmigrateshow that the primary key was deleted, an auto-increment column was added 'id', and it gets the primary key. I was offered the default value for the new column 'id'and mistakenly gave it 1, which was already in the table. Corresponding SQL:

ALTER TABLE `Host` ADD COLUMN `id` integer AUTO_INCREMENT DEFAULT 1 NOT NULL PRIMARY KEY;

and migration code:

    operations = [
    migrations.AddField(
        model_name='host',
        name='id',
        field=models.AutoField(auto_created=True, primary_key=True, default=1, serialize=False, verbose_name='ID'),
        preserve_default=False,
    ),

As a result, I can still change my models and it makemigrationsworks, but each command migrategives this error:

django.db.utils.OperationalError: (1067, "Invalid default value for 'id'")

and does not enter into force.

, ( ) . , / . , . ?

Edit: , , Django SQL ...AUTO_INCREMENT DEFAULT 1...?

+4
3

SQL ( ), ./manage.py migrate [your_app_name] [the_migration_number] --fake

--fake Django, .

, , SQL, , Django makemigrations.

id, , mysql.

+6

MySQL.

. , "" (Auto Generated by Django) Auto Increment = "True". Auto increment , 1. , script, -

,

ALTER TABLE YOU_TABLE CHANGE id id INT(11) AUTO_INCREMENT NOT NULL DEFAULT 1;

MySQL Database Error: Invalid default value for 'id'    1

, , .

ALTER TABLE YOU_TABLE CHANGE id id INT(11) AUTO_INCREMENT NOT NULL;

, , .

Django ,

column = models.CharField(max_length=7, default='0000000', editable=False)

Django: https://docs.djangoproject.com/en/dev/ref/models/fields/#editable StackOverflow Question: Django

0

primary_key , .

-1

Source: https://habr.com/ru/post/1568167/


All Articles