Django 3.2rc1: models.W042 is raised on inherited manually specified primary key.

343 views
Skip to first unread message

Caram

unread,
Mar 25, 2021, 9:28:09 AM3/25/21
to Django developers (Contributions to Django itself)

This issue was raised in alpha1, but I still had the issue in beta1 and now also in rc1.

As a result, I have to manually edit the migrations files and remove lines that alter fields to become BigField().

Has this really been fixed? Am I missing something obvious?

Caram

Tim Graham

unread,
Mar 25, 2021, 10:55:27 AM3/25/21
to Django developers (Contributions to Django itself)
Hi, It would be helpful to link to the ticket and/or explain the issue in more detail.

Caram

unread,
Mar 25, 2021, 11:58:43 AM3/25/21
to Django developers (Contributions to Django itself)
Sorry, I meant to do that. Here's the link to the ticket:

I get an output similar to that of the ticket when I run `./manage makemigrations`:

        HINT: Configure the DEFAULT_AUTO_FIELD setting or the AppConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
myapp.MyClass: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
        HINT: Configure the DEFAULT_AUTO_FIELD setting or the AppConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.

(duplicated about 30 times because I have many such classes)

The primary key for these models has not changed since Django 0.90 and there was no issue in Django 3.1.
It started output HINT messages in Django 3.2 beta 1.
I have the same behaviour in Django 3.2 rc 1.

When I try the suggested workaround from the ticket, i.e.:
   DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

the migrations files contain the following migrations which I neeed to remove manually:

        migrations.AlterField(
            model_name='myclass',
            name='id',
            field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
        ),

Caram

Mariusz Felisiak

unread,
Mar 25, 2021, 1:07:24 PM3/25/21
to Django developers (Contributions to Django itself)
Caram, I don't see any issue here. As far as I'm aware you're using the auto-created primary key. If you want to get rid of warnings and avoid unwanted migrations in the future, set DEFAULT_AUTO_FIELD to AutoField.

See release notes for more details.

#32367 is about inherited PKs, so it's not related with your report.

Best,
Mariusz

Caram Dache

unread,
Mar 26, 2021, 4:21:43 AM3/26/21
to django-d...@googlegroups.com
My bad, I should have read the documentation more thoroughly before.

However, when I set DEFAULT_AUTO_FIELD to BigAutoField and run the migration, I get errors like the following. This was what troubled me in the first place:

django.db.utils.IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails (`djpat`.`#sql-bb7_1e84`, CONSTRAINT `myapp_logentry_extension_id_8856aeec_fk` FOREIGN KEY (`extension_id`) REFERENCES `myapp_extension` (`id`))')

Caram

--
You received this message because you are subscribed to a topic in the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-developers/bwP-ft6aXdI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/475b0c2d-14f8-4a1e-8889-87783557cdc2n%40googlegroups.com.

Mariusz Felisiak

unread,
Mar 26, 2021, 6:30:44 AM3/26/21
to Django developers (Contributions to Django itself)
Can you provide a small project to reproduce?

Best,
Mariusz

Caram

unread,
Mar 26, 2021, 10:31:23 AM3/26/21
to Django developers (Contributions to Django itself)
I'm now discovering that some INT keys were subrepticely converting to BIGINT in the database without me noticing. And there is no BigAutoField in any of the migrations.

More worrying, there seem to have been several other unwanted changes:
1. NOT NULL changed to DEFAULT NULL (or the other way round for some models). The model has not changed in any way recently however.
2. FOREIGN KEY contraints gone missing. I'm currently going through an old mysql backup to get them back.

Cheers,
Caram

Mariusz Felisiak

unread,
Mar 26, 2021, 12:49:18 PM3/26/21
to Django developers (Contributions to Django itself)
Hi Caram,

    Django doesn't perform any DDL operations outside of migrations. You've probably set DEFAULT_AUTO_FIELD to BigAutoField, then you generated and run the migrations which caused some failures. We can check migration failures if you can provide a small project to reproduce.

Best,
Mariusz
Reply all
Reply to author
Forward
0 new messages