Skip to content

fix: mark HasOne as required when foreign key is NOT NULL (#486)#768

Open
SHYXIN wants to merge 3 commits into
jowilf:mainfrom
SHYXIN:fix/required-relationship-field
Open

fix: mark HasOne as required when foreign key is NOT NULL (#486)#768
SHYXIN wants to merge 3 commits into
jowilf:mainfrom
SHYXIN:fix/required-relationship-field

Conversation

@SHYXIN

@SHYXIN SHYXIN commented May 17, 2026

Copy link
Copy Markdown
Contributor

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, HasOne was always created with required=False regardless 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, set required=True on the HasOne field.

required = all(not col.nullable for col in attr.local_columns)
converted_fields.append(HasOne(attr.key, identity=identity, required=required))

Testing

  • tests/test_converters.py — 2 tests pass
  • tests/test_views.py — 14 tests pass
  • Manual verification with nullable and non-nullable FK columns

Additional

Includes a chore commit to fix pyproject.toml pytest config conflict that was blocking test execution.

SHYXIN and others added 3 commits May 17, 2026 16:15
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]'
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.

1 participant