tortoise-orm icon indicating copy to clipboard operation
tortoise-orm copied to clipboard

AttributeError: 'NoneType' object has no attribute 'id', trying to remove a foreign key from a row or set it to null.

Open adambirds opened this issue 3 years ago • 3 comments

Describe the bug A clear and concise description of what the bug is.

I need to be able to remove/null a foreign key. It is set to NOT NULL in the model. But seem to be having issues.

Ignoring exception in command: 'NoneType' object has no attribute 'id':
Traceback (most recent call last):
  File "/project/bombot/venv/lib/python3.10/site-packages/twitchio/ext/commands/core.py", line 217, in invoke
    await self._callback(*args, *context.args, **context.kwargs)
  File "/project/bombot/app/modules/cogs/bom_mod_commands.py", line 40, in remove
    await Player.get(name=playername).update(clan=None, enabled=False)
  File "/project/bombot/venv/lib/python3.10/site-packages/tortoise/queryset.py", line 1105, in __await__
    self._make_query()
  File "/project/bombot/venv/lib/python3.10/site-packages/tortoise/queryset.py", line 1082, in _make_query
    getattr(value, field_object.to_field_instance.model_field_name), None
AttributeError: 'NoneType' object has no attribute 'id'

To Reproduce Steps to reproduce the behavior, preferably a small code snippet.

models.py:

class Clan(Model):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=255, unique=True)
    tag = fields.CharField(max_length=4, unique=True)

class StatusManager(Manager):
    def get_queryset(self):
        return super(StatusManager, self).get_queryset().filter(enabled=True)

class Player(Model):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=255, unique=True)
    clan = fields.ForeignKeyField('models.Clan', related_name='players', null=True)
    enabled = fields.BooleanField(default=True)

    enabled_players = StatusManager()

    def is_enabled(self) -> bool:
        return self.enabled

commands.py:

@commands.command()
    async def remove(self, ctx: commands.Context, playername: str) -> None:
        """
        !remove command
        """
        if (await Player.get(name=playername).exists()):
            if ((await Player.get(name=playername)).is_enabled()):
                await Player.get(name=playername).update(clan=None, enabled=False)
                await ctx.send(f"Removed @{playername} from their clan!")
            else:
                await ctx.send(f"@{playername} is not in a Clan roster!")
        else:
            await ctx.send(f"@{playername} does not currently exist!")

Expected behavior A clear and concise description of what you expected to happen.

The field should be NULLED.

Additional context Add any other context about the problem here.

adambirds avatar Oct 07 '22 01:10 adambirds

Okay so after lots of trial and error and searching Django documentation I have fixed this by changing:

await Player.get(name=playername).update(clan=None, enabled=False)

to

await Player.get(name=playername).update(clan_id=None, enabled=False)

It would be useful if this was documented somewhere so its not too difficult to figure it out for others.

adambirds avatar Oct 07 '22 18:10 adambirds

Agree, this needs documentation

ryzhovalex avatar Oct 14 '24 08:10 ryzhovalex

Agree, this needs documentation

I doubt this will happen give I raised the issue pretty much exactly 2 years ago and its not happened yet.

adambirds avatar Oct 18 '24 19:10 adambirds