Skip to content

get_or_create in load_from_grds() creates duplicate missions when thumbnail_filename or notes_filename change between runs #314

Description

@ksalamy

In scripts/load.py line 1448, Mission.objects.get_or_create() includes thumbnail_filename, notes_filename, grid_bounds, and directory as lookup fields alongside name:

mission, created = Mission.objects.get_or_create(
    name=os.path.dirname(fp).replace(MBARI_DIR, ""),
    grid_bounds=grid_bounds,
    notes_filename=notes_filename,
    thumbnail_filename=thumbnail_filename,
    directory=os.path.dirname(fp),
)

Because all fields are part of the lookup key, if any field changes between bootstrap runs (e.g. thumbnail_filename is empty on the first run because locate hasn't indexed the file yet, then non-empty on a subsequent run), Django creates a second Mission with the same name rather than updating the existing one.

This causes cascading failures: the --spreadsheets phase calls Mission.objects.get(name=...) which crashes with MultipleObjectsReturned when duplicates exist.

Fix: Move all fields except name into a defaults={} dict, and use update_or_create so re-runs safely update rather than duplicate:

mission, created = Mission.objects.update_or_create(
    name=os.path.dirname(fp).replace(MBARI_DIR, ""),
    defaults={
        "grid_bounds": grid_bounds,
        "notes_filename": notes_filename,
        "thumbnail_filename": thumbnail_filename,
        "directory": os.path.dirname(fp),
    }
)

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions