Issue: get_or_none() fails with postgres
Author: TxMat
State: closed
Created: 2023-04-11 04:01:29

```py
# Get shipping info from order.shipping_info_id
shipping_info = {}
if order.shipping_info:
      shipping_info = model_to_dict(ShippingInfo.get_or_none(order.shipping_info))
      # no need to send id to client
       del shipping_info["id"]
```

Get_or_none with postgres raises `psycopg2.errors.DatatypeMismatch: argument of WHERE must be type boolean, not type integer` as the query built uses `WHERE 1`

here is the full sql query : 
```
SELECT "t1"."id", "t1"."country", "t1"."address", "t1"."postal_code", "t1"."city", "t1"."province" FROM "shippinginfo" AS "t1" WHERE 1 LIMIT 1 OFFSET 0'
```

I'm now using select and get as a temporary fix

```py
# Get shipping info from order.shipping_info_id
shipping_info = {}
if order.shipping_info:
      shipping_info = model_to_dict(ShippingInfo.select().where(ShippingInfo.id == order.shipping_info.id).get())
      # no need to send id to client
       del shipping_info["id"

```

Full stack trace :
---

```
functional_test.py:133 (TestPutShippingInfoOrder.test_put_order_valid_shipping_info)
self = <peewee.PostgresqlDatabase object at 0x7f3d953e5a10>
sql = 'SELECT "t1"."id", "t1"."country", "t1"."address", "t1"."postal_code", "t1"."city", "t1"."province" FROM "shippinginfo" AS "t1" WHERE %s LIMIT %s OFFSET %s'
params = [1, 1, 0], commit = None

    def execute_sql(self, sql, params=None, commit=None):
        if commit is not None:
            __deprecated__('"commit" has been deprecated and is a no-op.')
        logger.debug((sql, params))
        with __exception_wrapper__:
            cursor = self.cursor()
>           cursor.execute(sql, params or ())
E           psycopg2.errors.DatatypeMismatch: argument of WHERE must be type boolean, not type integer
E           LINE 1: ..."t1"."province" FROM "shippinginfo" AS "t1" WHERE 1 LIMIT 1 ...
E                                                                        ^

../../venv/lib/python3.11/site-packages/peewee.py:3236: DatatypeMismatch

During handling of the above exception, another exception occurred:

self = <Tests.functional_test.TestPutShippingInfoOrder object at 0x7f3d942c7310>
client = <FlaskClient <Flask 'api.inf349'>>

    def test_put_order_valid_shipping_info(self, client):
        create_order(client)
>       response = client.put('/order/1', json={
            "order": {
                "email": "elon.musk@spacex.com",
                "shipping_information": {
                    "country": "Senegal",
                    "address": "Rue des potiers",
                    "postal_code": "G7H 0S5",
                    "city": "Chicoutimi",
                    "province": "QC"
                }
            }
        })

functional_test.py:136: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../../venv/lib/python3.11/site-packages/werkzeug/test.py:1151: in put
    return self.open(*args, **kw)
../../venv/lib/python3.11/site-packages/flask/testing.py:223: in open
    response = super().open(
../../venv/lib/python3.11/site-packages/werkzeug/test.py:1095: in open
    response = self.run_wsgi_app(request.environ, buffered=buffered)
../../venv/lib/python3.11/site-packages/werkzeug/test.py:962: in run_wsgi_app
    rv = run_wsgi_app(self.application, environ, buffered=buffered)
../../venv/lib/python3.11/site-packages/werkzeug/test.py:1243: in run_wsgi_app
    app_rv = app(environ, start_response)
../../venv/lib/python3.11/site-packages/flask/app.py:2551: in __call__
    return self.wsgi_app(environ, start_response)
../../venv/lib/python3.11/site-packages/flask/app.py:2531: in wsgi_app
    response = self.handle_exception(e)
../../venv/lib/python3.11/site-packages/flask/app.py:2528: in wsgi_app
    response = self.full_dispatch_request()
../../venv/lib/python3.11/site-packages/flask/app.py:1825: in full_dispatch_request
    rv = self.handle_user_exception(e)
../../venv/lib/python3.11/site-packages/flask/app.py:1823: in full_dispatch_request
    rv = self.dispatch_request()
../../venv/lib/python3.11/site-packages/flask/app.py:1799: in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
../inf349.py:293: in order_id_handler
    return put_order()
../inf349.py:282: in put_order
    return update_shipping_order(payload["order"])
../inf349.py:217: in update_shipping_order
    return get_order()
../inf349.py:162: in get_order
    shipping_info = model_to_dict(ShippingInfo.get_or_none(order.shipping_info))
../../venv/lib/python3.11/site-packages/peewee.py:6634: in get_or_none
    return cls.get(*query, **filters)
../../venv/lib/python3.11/site-packages/peewee.py:6629: in get
    return sq.get()
../../venv/lib/python3.11/site-packages/peewee.py:7077: in get
    return clone.execute(database)[0]
../../venv/lib/python3.11/site-packages/peewee.py:1962: in inner
    return method(self, database, *args, **kwargs)
../../venv/lib/python3.11/site-packages/peewee.py:2033: in execute
    return self._execute(database)
../../venv/lib/python3.11/site-packages/peewee.py:2206: in _execute
    cursor = database.execute(self)
../../venv/lib/python3.11/site-packages/peewee.py:3244: in execute
    return self.execute_sql(sql, params)
../../venv/lib/python3.11/site-packages/peewee.py:3234: in execute_sql
    with __exception_wrapper__:
../../venv/lib/python3.11/site-packages/peewee.py:3010: in __exit__
    reraise(new_type, new_type(exc_value, *exc_args), traceback)
../../venv/lib/python3.11/site-packages/peewee.py:192: in reraise
    raise value.with_traceback(tb)

```

`get_or_none()` takes an expression. See docs: https://docs.peewee-orm.com/en/latest/peewee/api.html#Model.get_or_none

```python
ShippingInfo.get_or_none(
   ShippingInfo.id == order.shipping_info)
```

A cursory look would have solved this for you.

It indeed makes sense but for some reason the erroneous syntax Is totally valid when using an SQLite database witch threw me off a little bit.