django-postgres-extra icon indicating copy to clipboard operation
django-postgres-extra copied to clipboard

insert_and_get documentation is not clear

Open Amoki opened this issue 3 years ago • 0 comments

insert_and_get doesn't get anything when using ConflictAction.NOTHING and if there is a conflict.

The documentation is not very clear on this point https://django-postgres-extra.readthedocs.io/en/master/conflict_handling.html#insert-vs-insert-and-get On https://django-postgres-extra.readthedocs.io/en/master/conflict_handling.html#conflictaction-nothing, it says

This applies to both insert() and bulk_insert()

What about insert_and_get() ?

If it's the intended behavior, we may don't want to call the method _and_get if it can't get the data.

My workaround is

obj = MyObject.objects.on_conflict(
    ["column1", "column2"], ConflictAction.NOTHING
).insert_and_get(**validated_data)
if not obj:
    validated_data.pop("column3")  # This column is not relevant because of ConflictAction.NOTHING
    return MyObject.objects.get(**validated_data)

# The object has been added, no workaround needed
return obj

I find it very ugly, is there another way to do this ?

Amoki avatar Apr 08 '21 09:04 Amoki