question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get all results from a Select Query without specifying columns

See original GitHub issue

Hi, so far I’ve read majority of the documentation on peewee. While there are plenty of instructions on how to create a SelectQuery [1][2]. There is no example on extracting everything about the results (list of rows). One would expect a query statement from SQL return a table-like. I’ve come from using SQLalchemy expression language to peewee ORM. I have no background on Flask

Throughout reading the whole documentation and a whole page of google results I’ve never found the proper use of SelectQuery.

Goal: Extract a range of entries from SQL

Not very useful as it only returns PK

db = SqliteDatabase(...)

query = User.select().where(User.id.between(0,100))
cursor = db.execute(query) 
print(cursor.fetchone()) # prints primary key

If the previous one only return PK why does this return everything

query = User.select()
cursor = db.execute_sql()(*query) 
print(cursor.fetchone()) # prints a row as a tuple!!

Bad practice, but works perfectly 👍

query = User.select().where(User.id.between(0,100))
rows = [row._data for row in query]

Now I’m just being hopeful

import pandas as pd
query = User.select().where(User.id.between(0,100))
pd.read_sql(query.sql()) # this is obviously incorrect

Either I am very ignorant about how ORM work… actually SQLAchemy supports this:

…or there is some missing about peewee that the developers decided it is not within the scope of peewee to take care of. So is there a method from query that is on the lines of

query = User.select().where(User.id.between(0,100))
rows = query.all() # a list of results or rows, no need to be dictionary but preferable

Similar concerns:

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
coleifercommented, Mar 3, 2018

I’m not sure about your definition for the User model you’ve used in your examples, but presumably it has fields on it besides the ID?

Let’s look at some examples:

class User(Model):
    username = TextField()

The user model in these examples has a username field, as well as an implicit “id” field. As you will see, User.select() will select all the columns in the table by default. The query is iterable and the results it yields are, by default, instances of the model class. You can, if you like, get different objects from the cursor as the examples below show:

In [7]: query = User.select().where(User.id.between(0, 3))

In [8]: for user in query:
   ...:     print(user.id, user.username)
   ...:     
1 huey
2 charlie
3 mickey

In [9]: for user_dict in query.dicts():
   ...:     print(user_dict)
   ...:     
{'id': 1, 'username': 'huey'}
{'id': 2, 'username': 'charlie'}
{'id': 3, 'username': 'mickey'}

In [10]: for user_row in query.namedtuples():
    ...:     print(user_row)
    ...:     
Row(id=1, username='huey')
Row(id=2, username='charlie')
Row(id=3, username='mickey')

There’s no magic “.all()” you need… simply iterate over the Select query itself. If you explicitly want a cursor-wrapper (though there’s no need, as the query itself is fine to use), you can use the return value of Select.execute().

Does this answer your question?

PS - I’m finding it hard to believe you’ve read the docs if you are confused on this… it’s covered by the quick-start if you’d like a refresher.

2reactions
coleifercommented, Mar 3, 2018

Calling execute() on a select query returns a cursor-like object but it is not a db-api cursor. Again, not necessary as the query itself should be treated like a cursor.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Apply like over all columns without specifying ... - Stack Overflow
I have an idea of what data I'm looking for but I don't know what column it resides in and need to use...
Read more >
How to write SELECT * EXCEPT queries in Oracle Database
Remove columns by name or type from query results using Polymorphic Table Functions or SQL Macros.
Read more >
SQL SELECT without FROM should read VALUES without ...
Every SELECT needs a FROM clause according to the SQL standard. VALUES is like SELECT without FROM.
Read more >
Retrieving Rows with SELECT - Practical PostgreSQL [Book]
A SELECT statement may be as simple as a request for all rows and all columns from a specified table. Use the following...
Read more >
SELECT - Amazon Athena - AWS Documentation
EXCEPT returns the rows from the results of the first query, excluding the rows found by the second query. ALL causes all rows...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found