django-heralder has missing migrations when `DEFAULT_AUTO_FIELD` is not `django.db.models.AutoField`
Closed this issue · 5 comments
Steps to reproduce:
- Install
Django==4.1.6
anddjango-heralder==0.3.0
- Start a new Django 4.1.6 project with
django-admin startproject project_name
- Verify that the default value for
settings.DEFAULT_AUTO_FIELD
in the newly generated Django project isdjango.db.models.BigAutoField
- Add
herald
anddjango.contrib.sites
toINSTALLED_APPS
- Run
./manage.py makemigrations
Expected result:
Makemigrations would output that there are no changes detected.
Actual result:
A new migration is created with the following contents:
$ cat venv/lib/python3.11/site-packages/herald/migrations/0006_alter_notification_id_alter_sentnotification_id.py
# Generated by Django 4.1.6 on 2023-02-06 18:55
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('herald', '0005_auto_20180516_1755'),
]
operations = [
migrations.AlterField(
model_name='notification',
name='id',
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
migrations.AlterField(
model_name='sentnotification',
name='id',
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
]
This is to be expected -- Django changed the default_auto_field to BigAutoField
when generating a new app however the current default for backwards compatibility in settings is still AutoField
.
https://docs.djangoproject.com/en/4.1/releases/3.2/#customizing-type-of-auto-created-primary-keys
Maintaining the historical behavior, the default value for DEFAULT_AUTO_FIELD is AutoField. Starting with 3.2 new projects are generated with DEFAULT_AUTO_FIELD set to BigAutoField. Also, new apps are generated with AppConfig.default_auto_field set to BigAutoField. In a future Django release the default value of DEFAULT_AUTO_FIELD will be changed to BigAutoField.
To avoid unwanted migrations in the future, either explicitly set DEFAULT_AUTO_FIELD to AutoField:
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'
Yep, that is what I did for a newly generated Django project that used django-heralder
.
It would be great if you consider adding an app-level default_auto_field
so if users decide that they would prefer DEFAULT_AUTO_FIELD
to be a different kind of field other than AutoField
, django-heralder
's models would not be affected.
The folks over at Wagtail and Django-CMS have it set up that way so their core models' primary keys are independently configured from the user's project's settings.
I no longer help maintain this project because don't reliably have the ability to release on PyPi anymore. At DjangoCon US 2022 sprints, a group of us forked the project to Django-Heralder so we could do releases for bug fixes.
I created an issue that regarding what we are discussing here:
We welcome a PR that set the default_auto_field at the application level for Django-Heralder.
Heralder is also on PyPI - v.0.3.0 is equivalent to Django-Herald 0.3.0:
@b-ggs - totally got confused on which project I was commenting on -- this is Heralder... sigh
@b-ggs Your PR has been merged. Thanks for much for the contribution!