Skip to content

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

Open

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.

Activity

  1. waketzheng commented on Nov 22, 2025

    @waketzheng
    Contributor

    Can't reproduce with the following code:

    from __future__ import annotations
    
    from tortoise import Model, fields, run_async
    from tortoise.contrib.test import init_memory_sqlite
    
    
    class TUser(Model):
        id = fields.UUIDField(primary_key=True)
        name = fields.CharField(max_length=100)
    
        # For o2o relation, use BackwardOneToOneRelation or ModelClass instead of ReverseRelation
        user_information: VeryLongNameForTestingPurposesOnlyUserInfo
        short_user_info: fields.BackwardOneToOneRelation[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()
    
        def __repr__(self) -> str:
            return f"{self.__class__.__name__}(id={self.id}, not_too_long_user_age={self.not_too_long_user_age})"
    
    
    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()
    
    
    @init_memory_sqlite
    async def main():
        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(f"{short_info = }")
    
        print("Working")
        user = await TUser.filter(name="Test User").select_related("short_user_info").first()
        assert user
        user_short_info = user.short_user_info
        user_short_info.age = 31
        await user_short_info.save()
    
        print("Not Working")
        user = await TUser.filter(name="Test User").select_related("user_information").first()
        assert user
        user_info = user.user_information
        user_info.not_too_long_user_age = 32
        await user_info.save(
            # update_fields=["not_too_long_user_age"]
        )
        print(f"{await user_info.filter(id=user_info.id).first() = }")
    
    
    if __name__ == "__main__":
        run_async(main())
  2. rgabor-dev commented on Nov 24, 2025

    @rgabor-dev
    Author

    Hey @waketzheng, thanks for looking at it. It is happening when using a Postgres database. Probably because of the 64 char hard limit: https://www.postgresql.org/docs/current/runtime-config-preset.html#GUC-MAX-IDENTIFIER-LENGTH

  3. seladb commented on Mar 11, 2026

    @seladb
    Contributor

    I can confirm I managed to reproduce it with Postgres. I'm not familiar with the internals of Tortoise, but it seems this PR changed the behavior, specifically around this area:

    *[table[field].as_(f"{table.get_table_name()}.{field}") for field in data_fields]

    Maybe @henadzit can elaborate if we really need f"{table.get_table_name()}.{field}" ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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