Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyError: 'pk' when using Model.objects.get(pk=xxx) #34

Closed
foxmask opened this issue May 1, 2019 · 1 comment · Fixed by #37
Closed

KeyError: 'pk' when using Model.objects.get(pk=xxx) #34

foxmask opened this issue May 1, 2019 · 1 comment · Fixed by #37

Comments

@foxmask
Copy link
Contributor

foxmask commented May 1, 2019

I meet this error

Traceback (most recent call last):
  File "switch.py", line 24, in <module>
    loop.run_until_complete(switch(args.trigger_id))
  File "/usr/lib/python3.6/asyncio/base_events.py", line 473, in run_until_complete
    return future.result()
  File "switch.py", line 8, in switch
    trigger = await Trigger.objects.get(pk=trigger_id)
  File "/home/foxmask/DjangoVirtualEnv/yeoboseyo/lib/python3.6/site-packages/orm/models.py", line 182, in get
    return await self.filter(**kwargs).get()
  File "/home/foxmask/DjangoVirtualEnv/yeoboseyo/lib/python3.6/site-packages/orm/models.py", line 127, in filter
    column = self.table.columns[key]
  File "/home/foxmask/DjangoVirtualEnv/yeoboseyo/lib/python3.6/site-packages/sqlalchemy/util/_collections.py", line 194, in __getitem__
    return self._data[key]
KeyError: 'pk'

switch.py

# coding: utf-8
import argparse
import asyncio
from yeoboseyo.models import Trigger


async def switch(trigger_id):
    """

    :param trigger_id: trigger id to switch on/off
    :return:
    """
    trigger = await Trigger.objects.get(pk=trigger_id)
    status = not trigger.status
    await trigger.update(status=status)
    print(f"Successfully switched Trigger '{trigger.description}' to {status}")


if __name__ == '__main__':
    print('여보세요 ! Switch')
    parser = argparse.ArgumentParser(description='Switch status of one trigger')
    parser.add_argument('trigger_id',
                        metavar='N',
                        type=int,
                        help='provide the id of the trigger to switch on/off')
    args = parser.parse_args()

    loop = asyncio.get_event_loop()
    loop.run_until_complete(switch(args.trigger_id))
    loop.close()

and the models

class Trigger(orm.Model):
    __tablename__ = "trigger"
    __database__ = database
    __metadata__ = metadata

    id = orm.Integer(primary_key=True)
    rss_url = orm.String(max_length=255)
    joplin_folder = orm.String(max_length=80)
    description = orm.String(max_length=200)
    date_created = orm.DateTime(default=datetime.datetime.now)
    date_triggered = orm.DateTime(allow_null=True)
    status = orm.Boolean(default=False)
    result = orm.Text(allow_null=True)
    date_result = orm.DateTime(allow_null=True)
    provider_failed = orm.Integer(allow_null=True)
    consumer_failed = orm.Integer(allow_null=True)

if I use

    trigger = await Trigger.objects.get(id=trigger_id)

instead of the code above everything wokrs fine

    trigger = await Trigger.objects.get(pk=trigger_id)
@Lynskylate
Copy link
Contributor

Lynskylate commented May 1, 2019

I met the same problem as you.
The method model.objects.get if have arguments, it will use QuerySet's filter method.
I can't find any extra handle for pk in the filter method.
if the filter method's argument doesn't contain "__", it just returns self.table.columns[column_name].

if "__" in key:
    ...
else:
     op = "exact"
     column = self.table.columns[key] # models.py 126

Maybe we should handle the pk argument , like this

elif key == "pk":
  column = self.table.primary_key.columns[0]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants