Issue: fn.Max Usage
Author: NAN-CForadas
State: closed
Created: 2022-08-03 20:44:24

If I run this in pgsql 'SELECT Max("t1"."version") FROM "reviews" AS "t1" WHERE ("t1"."description" = 'NAN63831511');'

I get:

```
psql (14.4, server 13.6)
Type "help" for help.

qualintel=# SELECT Max("t1"."version") FROM "reviews" AS "t1" WHERE ("t1"."description" = 'NAN63831511');
 max 
-----
   2
(1 row)

qualintel=# 
```

but if I run it in Python, I get:

```
description = 'NAN63831511'
query = Review.select(fn.Max(Review.version)).where(Review.description == description).execute()

for q in query:
    print(q)

---------------------------------------------------------------------------
ReviewStatusTypeDoesNotExist              Traceback (most recent call last)
Input In [10], in <cell line: 1>()
      1 for q in query:
----> 2     print(q)

File ~/PycharmProjects/pee-wee-testing/models.py:224, in Review.__str__(self)
    223 def __str__(self):
--> 224     return f"description: {self.description}, review_status_type: {self.review_status_type}" \
    225            f"user: {self.user}, version: {self.version}"

File ~/PycharmProjects/pee-wee-testing/venv/lib/python3.10/site-packages/peewee.py:4486, in ForeignKeyAccessor.__get__(self, instance, instance_type)
   4484 def __get__(self, instance, instance_type=None):
   4485     if instance is not None:
-> 4486         return self.get_rel_instance(instance)
   4487     return self.field

File ~/PycharmProjects/pee-wee-testing/venv/lib/python3.10/site-packages/peewee.py:4481, in ForeignKeyAccessor.get_rel_instance(self, instance)
   4479     return instance.__rel__.get(self.name, value)
   4480 elif not self.field.null and self.field.lazy_load:
-> 4481     raise self.rel_model.DoesNotExist
   4482 return value

ReviewStatusTypeDoesNotExist:
```

my models look like this:

```
lass ReviewStatusType(BaseModel):
    description = CharField(unique=True, index=True, null=False,
                            help_text='Review Status Type')

    class Meta:
        table_name = 'review_status_types'


class Review(BaseModel):
    description = CharField(index=True, null=False,
                            help_text='AScope File# and Revision')
    review_status_type = ForeignKeyField(column_name='review_status_type_id', field='id', model=ReviewStatusType,
                                         help_text='Related review status type id')
    user = ForeignKeyField(column_name='user_id', field='id', model=User,
                           help_text='Related user id')
    version = IntegerField(null=True,
                           help_text='Version number of review')
    file_no = CharField(index=True, null=True, default='',
                        help_text='File# e.g. NAN0000000')
    external_id = CharField(index=True, null=True, default='',
                            help_text='External id e.g. AScope id')

    class Meta:
        table_name = 'reviews'
```

what am I doing wrong?

```
class BaseModel(Model):
    class Meta:
        database = db
```

Please post on StackOverflow in the future. Questions like this are not appropriate to the issue tracker.

Docs: http://docs.peewee-orm.com/en/latest/peewee/query_operators.html#sql-functions

Your specific error is because your `Review.__str__` method, which you're calling when you iterate over your query results and print them, does a database lookup. Since you did not select the related column (`review_status_type`) we weren't able to look up the status and hence could not print. The error even states ReviewStatusTypeDoesNotExist...

Here's the copy/paste answer you probably are looking for:

```python
description = 'NAN63831511'

# Single value.
max_ver = (Review
           .select(fn.Max(Review.version))
           .where(Review.description == description)
           .scalar())
```

Thank you, much appreciated.