piccolo-orm / piccolo

A fast, user friendly ORM and query builder which supports asyncio.
https://piccolo-orm.com/
MIT License
1.46k stars 91 forks source link

Docs indicate that "first" and "get are identical, but they're not #1082

Closed trondhindenes closed 2 months ago

trondhindenes commented 2 months ago

Looking at https://piccolo-orm.readthedocs.io/en/latest/piccolo/query_types/objects.html#fetching-objects, there's nothing to hint to the user that the usage of "get" vs "first" are different. However, their usage is:

In [2]: await Band.objects().get(Band.name == 'Pythonistas')
Out[2]: <Band: 1>

In [3]: await Band.objects().first(Band.name == 'Pythonistas')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[3], line 1
----> 1 await Band.objects().first(Band.name == 'Pythonistas')

TypeError: Objects.first() takes 1 positional argument but 2 were given

In [4]: 

It would be good if the docs were expanded with where clauses to give the reader a sense of real-life usage of these methods.

dantownsend commented 2 months ago

OK, good point - I think that part of the docs can be expanded upon.

With the first method, the error was because:

# The `first` method doesn't take any args - so this will error:
>>> await Band.objects().first(Band.name == 'Pythonistas')

# The filtering has to be in a where clause:
>>> await Band.objects().where(Band.name == 'Pythonistas').first()
dantownsend commented 2 months ago

I've made a few changes to this page:

https://piccolo-orm.readthedocs.io/en/latest/piccolo/query_types/objects.html

Let me know if this is any better.

trondhindenes commented 2 months ago

awesome! Yeah I think this will help a lot!