Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sqlmodel/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
| Mapping[int, Union["IncEx", bool]]
| Mapping[str, Union["IncEx", bool]]
)
OnDeleteType = Literal["CASCADE", "SET NULL", "RESTRICT"]
OnDeleteType = Literal["CASCADE", "SET NULL", "SET DEFAULT", "RESTRICT", "NO ACTION"]


def __dataclass_transform__(
Expand Down
24 changes: 24 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import RelationshipProperty
from sqlalchemy.schema import CreateTable
from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select


Expand Down Expand Up @@ -216,3 +217,26 @@ class Hero(SQLModel, table=True):
assert len(foreign_keys) == 1
assert foreign_keys[0].ondelete == "CASCADE"
assert team_id_column.nullable is False


@pytest.mark.parametrize("ondelete", ["SET DEFAULT", "NO ACTION"])
def test_foreign_key_ondelete_referential_actions(clear_sqlmodel, ondelete):
class Team(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)

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=ondelete
)

engine = create_engine("sqlite://")
SQLModel.metadata.create_all(engine)

hero_table = Hero.__table__ # type: ignore[attr-defined]
foreign_keys = list(hero_table.c.team_id.foreign_keys)
assert len(foreign_keys) == 1
assert foreign_keys[0].ondelete == ondelete

ddl = str(CreateTable(hero_table).compile(engine))
assert f"ON DELETE {ondelete}" in ddl
Loading