hwjp
hwjp

Reputation: 16071

wagtail 'Page with this Path already exists.' error on attempting to create page manually

I have some code to create a Custom page object as part of a data import:

instance = PerformancePage(
    run=run,
    date=json_data['date'],
    time=json_data['time'],
    price=json_data['price'],
    title=f'{run.title} {json_data["date"]} {json_data["id"]}',
    content_type=ContentType.objects.get_for_model(PerformancePage)
)
perf = run.add_child(instance=instance)

and that sometimes raises:

django.core.exceptions.ValidationError: {'path': ['Page with this Path already exists.']}

A bit of debug code does show that there is another page out there with the same path:

except ValidationError:
    print('error attempting to save', instance)
    print('path', instance.path)
    print('is leaf', run.is_leaf())
    rivals = Page.objects.filter(path=instance.path)
    print(rivals.last().specific.run == run)

Why might that be?

trying to manually increment the rival path to set a new path doesn't work either:

instance.path = rivals.last().specific._inc_path()
perf = run.add_child(instance=instance)
# still raises

Interestingly, if I just skip over those exceptions and continue my import, when I print out those paths, they seem to follow a similar pattern:

path 00010001000T0005000D0001
path 00010001000T000800050001
path 00010001000T000900060001
path 00010001000T000A00050001
path 00010001000T000A00050001
path 00010001000T000A00070001
path 00010001000T000A00070001
path 00010001000T000A00030001

could that be relevant?

Upvotes: 2

Views: 1062

Answers (1)

hwjp
hwjp

Reputation: 16071

Looks like the in-memory parent "run" object was out of date. re-fetching it from the database before trying to add the child fixes the problem:

 run = RunPage.objects.get(id=run.id)

Upvotes: 1

Related Questions