Skip to content

Misbehaving model save() when names are too long #2018

@rgabor-dev

Description

@rgabor-dev

Describe the bug
Model save() is not working when the model name and field name is too long and select_related is used.

To Reproduce

Tortoise ORM version: 0.25.1

Creating the test models:

from tortoise.models import Model
from tortoise import fields

class TUser(Model):
    id = fields.UUIDField(primary_key=True)
    name = fields.CharField(max_length=100)

    user_information: fields.ReverseRelation["models.VeryLongNameForTestingPurposesOnlyUserInfo"]
    short_user_info: fields.ReverseRelation["models.ShortUserInfo"]


class VeryLongNameForTestingPurposesOnlyUserInfo(Model):
    id = fields.UUIDField(primary_key=True)
    user: fields.OneToOneNullableRelation[TUser] = fields.OneToOneField(
        model_name="models.TUser",
        to_field="id",
        related_name="user_information"
    )
    not_too_long_user_age = fields.IntField()


class ShortUserInfo(Model):
    id = fields.UUIDField(primary_key=True)
    user: fields.OneToOneNullableRelation[TUser] = fields.OneToOneField(
        model_name="models.TUser",
        to_field="id",
        related_name="short_user_info"
    )
    age = fields.IntField()

Reproducing the issue:

    print("Creating user and user info")
    user = await TUser.create(name="Test User")
    user_info = await VeryLongNameForTestingPurposesOnlyUserInfo.create(
        user_id=user.id,
        not_too_long_user_age=30,
    )
    short_info = await ShortUserInfo.create(
        user_id=user.id,
        age=30,
    )

    print("Working")
    user = await TUser.filter(name="Test User").select_related("short_user_info").first()
    user_info = user.short_user_info
    user_info.age = 31
    await user_info.save()
    
    print("Not Working")
    user = await TUser.filter(name="Test User").select_related("user_information").first()
    user_info = user.user_information
    user_info.not_too_long_user_age = 32
    await user_info.save(
        # update_fields=["not_too_long_user_age"]
    )

This will raise:

IncompleteInstanceError: VeryLongNameForTestingPurposesOnlyUserInfo is a partial model, can only be saved with the relevant update_field provided

(Saving with update_fields works.)

Expected behavior
Saving with select_related should work regardless of the model name length.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions