furious-luke / django-address

A Django address model and field. Addresses may be specified by address components or by performing an automatic Google Maps lookup.
BSD 3-Clause "New" or "Revised" License
431 stars 181 forks source link

Nonexistent parent node ('address', '004_auto_20220227_1259') #168

Closed daniel-butler closed 2 years ago

daniel-butler commented 2 years ago

django-address version: 0.2.6

When creating a new project I am getting the error below when running python manage.py migrate. I've removed the pyc files and deleted all migration files and recreated them using makemigrations a few times.

I'm thinking that the django-address release doesn't have the migration? But then how does the migration in my project app know the name and value of it.

settings.py


INSTALLED_APPS = [
    ...
    "address",
    ....
]

GOOGLE_API_KEY = env("GOOGLE_API_KEY", default="TO_BE_FILLED_IN")

Migration file

class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ('address', '0004_auto_20220227_1259'),
    ]

    ...

When running migrations it is updating the id field.

Migrations for 'address':
  /usr/local/lib/python3.9/site-packages/address/migrations/0004_auto_20220412_0727.py
    - Alter field id on address
    - Alter field id on country
    - Alter field id on locality
    - Alter field id on state

Error

django.db.migrations.exceptions.NodeNotFoundError: Migration <project_app>.0001_initial dependencies reference nonexistent parent node ('address', '0004_auto_20220227_1259')
daniel-butler commented 2 years ago

I honestly don't know how I got that migration number. When I looked through the current develop branch and the one tag with the version number I found the latest migration to be 0003_auto_20200830_1851 when I updated the migration to use that file it worked.

https://github.com/furious-luke/django-address/tree/develop/address/migrations

furious-luke commented 2 years ago

Hi @daniel-butler! That sounds pretty frustrating. If you were to start a new project do you still get the same error? One thing to check might be that your database is empty before trying again. Django stores details about which migrations have been run, it could be conceivable that it's getting confused by bogus data in the migration table (however that's just a guess).

Please let me know if you're still experiencing issues, we'll dig into it.

daniel-butler commented 2 years ago

It happened again when I recreated the migrations. It is possible I am doing something wrong, though.

sacovo commented 2 years ago

Could this be the issue? https://stackoverflow.com/questions/67006488/migrating-models-of-dependencies-when-changing-default-auto-field

daniel-butler commented 2 years ago

I think that is it. I'll try it tomorrow!

daniel-butler commented 2 years ago

That was the issue! This was the fix

settings.py


INSTALLED_APPS = [
    ...
    "project.apps.ModifiedAddressConfig",
    ...
]

project/apps.py

"""
Defines app configurations for dependency files to avoid unnecessary migrations

More details: https://stackoverflow.com/a/67007098/7838574
"""
from address.apps import AddressConfig

class ModifiedAddressConfig(AddressConfig):
    default_auto_field = "django.db.models.AutoField"
furious-luke commented 2 years ago

Hey @daniel-butler! That certainly explains why you're getting an additional migration created within Django Address, however it's not clear to me that this explains why there is a non-existant parent migration file, which I think is the more pressing concern.

I've created the same situation locally (i.e. I've set DEFAULT_AUTO_FIELD to BigAutoField instead of AutoField) and can cause Django to trigger creating a new migration, however it also migrates correctly:

image

So, it seems that for some reason the generated migration file in your setup is getting removed. Can you think of a reason why that might be happening?

daniel-butler commented 2 years ago

Yeah, I think, it is because I added Django-address after I had a few migrations in the project. When creating the new migration with addresses is when I ran into the issue of have a non-existing migration being a parent of another.

Thinking about it, I would expect to continue to run into the problem, but it only happened once.

Does that help?

furious-luke commented 2 years ago

Yeah, I think, it is because I added Django-address after I had a few migrations in the project. When creating the new migration with addresses is when I ran into the issue of have a non-existing migration being a parent of another.

That's the process I used to generate the above output I attached, so not sure if that's the cause.

The only situation I can think of that might account for the issue you were seeing is if at some point you ran makemigrations and it generated the additional migration file in Django Address, and also added a dependency to that migration in one of your own migrations in your Django project, and then subsequent to that, that new migration file in Address was deleted. Then, the next time you ran makemigrations that migration in Address would be created again, but with a new filename, thus causing the missing migration.

From your initial post, it looks like the missing migration name was located in project_app, was your Django project called project by any chance?

daniel-butler commented 2 years ago

No, it's not called project. I just changed the name to that.

Do you have a link you could share for the project that generated the migrations above? I'm missing the part where there are migrations in the project before address is added to the project.

I am also using django-tenants which could be attributing to the problem because migrations are split between the public tenant and all others.

furious-luke commented 2 years ago

Do you have a link you could share for the project that generated the migrations above?

Nope, sorry, I just threw it together based on the example site in Django Address.

As an alternative, could you create a minimal example that reproduces the issue you're seeing?

furious-luke commented 2 years ago

Actually, scratch that, after thinking a little more about it it makes sense that you'd get an error in certain circumstances. I believe the error occurs as a result of the workflow I mentioned before, and I think the reason the newly created migration in Django Address gets deleted is due most likely to reinstalling the dependencies/virtual environment.

I'll raise a PR to correct the issue and link to this issue. Thanks @daniel-butler!

AntoineHessAcernis commented 2 years ago

django-address version: 0.2.6

Hello guys ! I think I am facing the same problem here.

Do you think you will raise a PR soon ? I did not really understand the problem but I also have a depedency generated like this :

dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('contenttypes', '0002_remove_content_type_name'),
        ('address', '0004_alter_address_id_alter_country_id_alter_locality_id_and_more'),
    ]

I get this error when I migrate :

raise NodeNotFoundError(self.error_message, self.key, origin=self.origin) django.db.migrations.exceptions.NodeNotFoundError: Migration idt.0001_initial dependencies reference nonexistent parent node ('address', '0004_alter_address_id_alter_country_id_alter_locality_id_and_more')

I tried the solutions explained before but none of them worked.

furious-luke commented 2 years ago

Hi @AntoineHessAcernis and @daniel-butler! My apologies for the long delay, I've just published the latest version of Django Address containing the necessary fix to this issue. Please give it a go and let me know if anything goes awry.

daniel-butler commented 2 years ago

Thank you for fixing, and all the work in this project!

AntoineHessAcernis commented 2 years ago

Thank you @furious-luke, it works for me.