tortoise / tortoise/tortoise-orm

No support for on_delete=SET_DEFAULT in ForeignKeyField

Open
#584 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

Describe the bug
Creating a ForeignKeyField with on_delete=SET_DEFAULT results in an error
stating on_delete must be set to CASCADE, RESTRICT or SET_NULL

To Reproduce

from tortoise import fields
from tortoise.models import Model

class User(Model):
    id = fields.IntField(pk=True)
    username = fields.CharField(max_length=32)
    team = fields.ForeignKeyField('models.Team', on_delete=fields.SET_DEFAULT, default=1)

>>>
../../../Library/Caches/pypoetry/virtualenvs/banyan-f68Y3YIt-py3.8/lib/python3.8/site-packages/tortoise/fields/relational.py:496: in ForeignKeyField
    return ForeignKeyFieldInstance(
../../../Library/Caches/pypoetry/virtualenvs/banyan-f68Y3YIt-py3.8/lib/python3.8/site-packages/tortoise/fields/relational.py:331: in __init__
    raise ConfigurationError("on_delete can only be CASCADE, RESTRICT or SET_NULL")
E   tortoise.exceptions.ConfigurationError: on_delete can only be CASCADE, RESTRICT or SET_NULL

Expected behavior
Tortoise documentation lists SET_DEFAULT as an acceptable value as long as default= is set for the field.

on_delete:
    One of:
        field.CASCADE:
            Indicate that the model should be cascade deleted if related model gets deleted.
        field.RESTRICT:
            Indicate that the related model delete will be restricted as long as a foreign key points to it.
        field.SET_NULL:
            Resets the field to NULL in case the related model gets deleted. Can only be set if field has null=True set.
        field.SET_DEFAULT:
            Resets the field to default value in case the related model gets deleted. Can only be set is field has a default set.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in tortoise/fields/relational.py at ForeignKeyFieldInstance, where the reported ConfigurationError is raised. Compare the documented SET_DEFAULT contract with the existing ForeignKeyField validation and reproduce the supplied User/Team example; done means SET_DEFAULT with a default is accepted without the current error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.