Skip to content

fix: default relationship fields to required=False to avoid missing data errors on load (#664) - #745

Open
Fire162 wants to merge 1 commit into
marshmallow-code:devfrom
Fire162:fix/relationship-field-required-handling
Open

Fire162 wants to merge 1 commit into
marshmallow-code:devfrom
Fire162:fix/relationship-field-required-handling

Conversation

@Fire162

@Fire162 Fire162 commented Oct 6, 2026

Copy link
Copy Markdown

Summary

Fixes #664.

In #645, relationship fields generated from non-nullable foreign keys were marked with required: not nullable (i.e. required=True). While allow_none=False correctly prevents assigning None to non-nullable relationships, marking relationship fields as required=True by default breaks deserialization when:

  1. include_relationships=True and include_fk=True are both active, requiring callers to redundantly provide both the foreign key ID and the related object.
  2. Deserializing nested objects where child models reference parent models whose foreign key identity has not yet been committed to the database.
  3. Loading models where only scalar/foreign key fields are supplied in the deserialization payload.

Changes

  • Sets relationship fields generated by ModelConverter._add_relationship_kwargs to required=False while preserving allow_none=nullable (so setting None on non-nullable relationships remains rejected).
  • Guards Related._get_existing_instance when all primary key lookup_values are None, raising NoResultFound without triggering SQLAlchemy's SAWarning: fully NULL primary key identity cannot load any object.
  • Updates test_conversion.py and adds regression tests in test_sqlalchemy_schema.py verifying deserialization with non-nullable relationships and warning prevention.

…g on null keys (marshmallow-code#664)

- Set default required=False on relationship fields generated by ModelConverter
  while keeping allow_none matching relationship nullability.
- Prevent ValidationError ('Missing data for required field') when deserializing
  models with include_relationships=True using foreign key values or nested data.
- Avoid SAWarning in Related._get_existing_instance when all lookup values are None.
- Add regression tests for schema.load with non-nullable relationships and warning prevention.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

I suspect a breaking change related to "include_relationships=True" and schema.load() in version 1.4.1

1 participant