Open EvSpirit opened 4 months ago
This pull request addresses issues related to nested fields updates and refines the handling of Django's get_or_create
shortcut. The changes include bug fixes, enhancements, and code refactoring. The main approach involves using get
before create
to avoid unique constraint violations and introducing a convention to force object creation when a None
primary key is explicitly passed. Additionally, the PR includes new tests, schema updates, and minor code cleanups.
Files | Changes |
---|---|
tests/test_input_mutations.py tests/projects/schema.py tests/projects/test_schema.py tests/projects/models.py tests/mutations/test_mutations.py tests/projects/schema_inherited.py tests/types.py |
Added new test cases, updated existing tests, and modified schema and model definitions to support new nested relations and input types. |
strawberry_django/mutations/resolvers.py strawberry_django/mutations/mutations.py strawberry_django/mutations/types.py strawberry_django/mutations/fields.py |
Refactored mutation resolver functions to handle None and UNSET primary key values, introduced full_clean options, and improved nested object creation handling. |
I'd love to see this merged. Happy to help where I can. 😄
I'd love to see this merged. Happy to help where I can. 😄
I'm mostly waiting for the tests to pass and the adjustment to the conflict to review this again.
Not sure if @EvSpirit is going to tackle those, but if not, I do appreciate the help to finish this PR and merge it :)
Description
This PR addresses issues described in #603. Kindly ask you to review the code and share your ideas.
While working on these two major bugs I also faced another issue related to previously added Django
get_or_create
shortcut (see PR #362). The current implementation seems a bit inconsistent for me becauseget_or_create
is used only in m2m relations, more than that, this shortcut relies not only on unique fields. So it definitely fixes the issue mentioned in #360 , but at the same time there may be an opposite situation when duplicated records won't be created. First I thought to identify all unique fields and constraints (e.g. using Djangototal_unique_constraints
) to further understand ifget
should be used before creating an object. But it seems there are quite many caveats. So what I'm suggesting is to have the following convention: if an object input contains explicitly passedNone
/null
pk value (notUNSET
), it would indicate an intention to force create an object. If omitted,get_or_create
approach will be applied (actually, it's a bit different under the hood, I refactoredfull_clean
usage to be invoked only afterget()
, otherwise,full_clean
may trigger unique constraint violation).Personally I think that it should not be hardcoded somewhere in the lib and this approach looks only as a workaround for me, probably it's a good idea to further extend input types or probably integrate forms to precisely control fields cleaning and saving, that's for further discussion and may be a a good feature request (as while working on the issue I noticed that
full_clean
options usability may be limited in case of nested relations).It the current idea looks good, I think it would be better to update docs respectively.
Other than proposed fixes for the issues mentioned above, there is some minor code refactoring and cleanup:
full_clean
options exposed toDjangoMutationCUD
Please let me know if it is better to move out such changes to another PR.
Types of Changes
Issues Fixed or Closed by This PR
603
Checklist
Summary by Sourcery
This pull request fixes issues with nested fields update, refines the
full_clean
process, and enhances the handling ofget_or_create
for better consistency. It also includes new tests to cover these changes and removes redundant test code.full_clean
usage to be invoked only afterget()
to prevent unique constraint violations.full_clean
options toDjangoMutationCUD
for better control over validation.get_or_create
to support explicitNone
/null
pk values for forced object creation.