Skip to content

Add SET DEFAULT and NO ACTION to OnDeleteType#2034

Closed
anxkhn wants to merge 1 commit into
fastapi:mainfrom
anxkhn:patch-4
Closed

Add SET DEFAULT and NO ACTION to OnDeleteType#2034
anxkhn wants to merge 1 commit into
fastapi:mainfrom
anxkhn:patch-4

Conversation

@anxkhn

@anxkhn anxkhn commented Jul 6, 2026

Copy link
Copy Markdown

OnDeleteType, the Literal that types the public Field(ondelete=...)
parameter, only listed three of the five standard SQL referential actions:
CASCADE, SET NULL and RESTRICT. The remaining two, SET DEFAULT and
NO ACTION, are valid everywhere else in the stack: ondelete is passed
straight through to SQLAlchemy's ForeignKey(ondelete=...), which emits
ON DELETE <value> for any of them, and SQL defines exactly these five actions.

Because the literal omitted them, a type checker (mypy/pyright/ty) rejected a
perfectly valid schema, for example:

class Hero(SQLModel, table=True):
    id: int | None = Field(default=None, primary_key=True)
    team_id: int | None = Field(
        default=None, foreign_key="team.id", ondelete="NO ACTION"
    )

The only workarounds were casting to Any or dropping down to sa_column.
NO ACTION is the default referential action in PostgreSQL and most databases,
and SET DEFAULT pairs naturally with a column server_default, so both are
things people legitimately want to spell out.

This adds the two missing actions to OnDeleteType. There is no runtime change
(the value was always passed through unchanged); this only corrects the static
type. Reported in #1839.

Tests

Added a parametrized regression test over the two newly typed actions that
asserts both the resulting foreign key's ondelete and the generated
ON DELETE <action> DDL, mirroring the existing
test_foreign_key_ondelete_with_annotated.

The OnDeleteType literal that types Field(ondelete=...) only listed
CASCADE, SET NULL and RESTRICT, so type checkers rejected the other two
standard SQL referential actions, SET DEFAULT and NO ACTION, even though
both are valid at runtime and emit correct ON DELETE DDL via SQLAlchemy.

Add the two missing actions to the literal and cover them with a
parametrized regression test asserting the generated foreign key and
DDL.
@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

This was marked as potentially AI generated and will be closed now. If this is an error, please provide additional details, make sure to read the docs about contributing and AI.

@github-actions github-actions Bot closed this Jul 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants