# Is this a bug in python-sql?

**URL:** https://discuss.tryton.org/t/is-this-a-bug-in-python-sql/8546
**Category:** Developer
**Tags:** python-sql
**Created:** [May 1, 2025, 10:57am UTC](https://discuss.tryton.org/t/is-this-a-bug-in-python-sql/8546 "2025-05-01T10:57:54Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![bell](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/b/c67d28/32.png) [@bell](https://discuss.tryton.org/u/bell)
#### Post date: [May 1, 2025, 10:57am UTC](https://discuss.tryton.org/t/is-this-a-bug-in-python-sql/8546/1 "2025-05-01T10:57:54Z")

</div>

Hi!  
I noticed that my sql-query is not being executed correctly.  
I have modeled a simple example:

```auto
    user = Table('res_user')
    group = user.create_uid
    total = Sum(Case((user.write_uid == 2, 3), else_=4))

    query = user.select(
        group.as_('group'),
        total.as_('total'),
        group_by=[user.create_uid],
        having=(total != 0),
        order_by=[Literal(1)],
    )

    logger.info('query = %s', str(query) % query.params)
    logger.info('query.params = %s', query.params)

```

And this is what I got:

```auto
query = SELECT "a"."create_uid" AS "group", 
SUM(CASE WHEN ("a"."write_uid" = 2) THEN 3 ELSE 4 END) AS "total" 
FROM "res_user" AS "a" 
GROUP BY "a"."create_uid" 
HAVING (SUM(CASE WHEN ("a"."write_uid" = 1) THEN 2 ELSE 3 END) != 4) 
ORDER BY 0

query.params = (2, 3, 4, 1, 2, 3, 4, 0)

```

But the result should be like this:

`query.params = (2, 3, 4, 2, 3, 4, 0, 1)`

If you replace` order_by=[Literal(1)]` with `order_by=[group]`, the query will be executed correctly:

`query.params = (2, 3, 4, 2, 3, 4, 0)`

But if you then replace `group = user.create_uid` with `group = Coalesce(user.create_uid, 1)`, the query will again be executed incorrectly:

`query.params = (1, 2, 3, 4, 1, 2, 3, 4, 0)`

P.S. The version of `python-sql` is 1.5.2

---

<div class="post-metadata">

### Author: ![bell](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/b/c67d28/32.png) [@bell](https://discuss.tryton.org/u/bell)
#### Post date: [May 1, 2025, 12:29pm UTC](https://discuss.tryton.org/t/is-this-a-bug-in-python-sql/8546/2 "2025-05-01T12:29:37Z")

</div>

If you delete `order_by` completely, all queries will be processed correctly.

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [May 1, 2025, 12:33pm UTC](https://discuss.tryton.org/t/is-this-a-bug-in-python-sql/8546/3 "2025-05-01T12:33:06Z")

</div>

Yes it is a bug that should be fixed.

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [May 1, 2025, 5:11pm UTC](https://discuss.tryton.org/t/is-this-a-bug-in-python-sql/8546/4 "2025-05-01T17:11:56Z")

</div>

Here is [Wrong parameter position on Select with order\_by and having or windows (#94) · Issues · Tryton / python-sql · GitLab](https://foss.heptapod.net/tryton/python-sql/-/issues/94)
