fix: Add missing group_id to RecipeTag and TagBase schemas #5342
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem:
When updating recipe tags via the API (
PATCH /api/recipes/{recipe_slug}
or using bulk actions like/api/recipes/bulk-actions/tag
), requests can fail with either aTypeError: __init__() missing 1 required positional argument: 'group_id'
or aSQL Integrity Error
(violating the unique constraint ontags.slug
+tags.group_id
).Root Cause:
There is an inconsistency between the Pydantic schemas used for API validation and the underlying SQLAlchemy database model for Tags:
Tag
database model (mealie/db/models/recipe/tag.py
) correctly requiresgroup_id
during initialization (__init__
).RecipeTag
(mealie/schema/recipe/recipe.py
) andTagBase
(mealie/schema/recipe/recipe_category.py
), which are used to validate thetags
array in API request payloads, do not define agroup_id
field.group_id
within the tag objects is sent, Pydantic strips this field during validation.group_id
, leading to errors when attempting to interact with theTag
database model.Solution:
This PR adds the missing
group_id: UUID4 | None = None
field to theRecipeTag
andCategoryBase
(whichTagBase
inherits from) Pydantic schemas.mealie/schema/recipe/recipe.py
(modifiedRecipeTag
)mealie/schema/recipe/recipe_category.py
(modifiedCategoryBase
)This ensures that
group_id
, if provided in an API request for associating tags, is correctly validated and passed through to the backend database logic, resolving theTypeError
andSQL Integrity Error
. The field is made optional (| None = None
) to minimize potential side effects in other parts of the code that might use these schemas without providing a group ID.However, this change broke tests. Fixing it required adding the required group_id field to the RecipeToolOut schema. This ensures group_id is present when validating Recipe objects during creation in tests, resolving Pydantic validation errors and downstream IntegrityErrors that occurred during test setup fixtures.
Testing:
Tested locally by applying these schema changes and using an external script to PATCH recipes with tags including
id
,name
,slug
, andgroup_id
. The previousTypeError
andSQL Integrity Error
were resolved, and tags were associated successfully.Ran the python test suite locally, all tests are now passing.
Fixes #5286