fix: mark HasOne as required when foreign key is NOT NULL (#486)#768
Open
SHYXIN wants to merge 3 commits into
Open
fix: mark HasOne as required when foreign key is NOT NULL (#486)#768SHYXIN wants to merge 3 commits into
SHYXIN wants to merge 3 commits into
Conversation
When a MANYTOONE relationship's local foreign key column is NOT NULL, the HasOne field is now automatically marked as required. Previously, HasOne fields were always created with required=False regardless of the column's nullable setting. The fix checks all local columns of the RelationshipProperty. If all local columns are NOT NULL, required is set to True.
Remove duplicate [tool.pytest] section that conflicts with [tool.pytest.ini_options], causing pytest to fail with: 'Cannot use both [tool.pytest] and [tool.pytest.ini_options]'
for more information, see https://pre-commit.ci
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
When a MANYTOONE relationship's local foreign key column is NOT NULL, the HasOne field was not marked as required in forms. This caused the form to not show the required indicator and not enforce the constraint.
Root Cause
In
BaseSQLAModelConverter.convert_fields_list,HasOnewas always created withrequired=Falseregardless of the foreign key column's nullable setting.Fix
starlette_admin/contrib/sqla/converters.py:107-113— Check all local columns of the RelationshipProperty. If all local columns are NOT NULL, setrequired=Trueon the HasOne field.Testing
tests/test_converters.py— 2 tests passtests/test_views.py— 14 tests passAdditional
Includes a chore commit to fix pyproject.toml pytest config conflict that was blocking test execution.