[0.15.22] asyncpg error: Column "X" must appear in group by clause or be used in an aggregate function
See original GitHub issueHi,
Recently encountered an issue with newer versions of tortoise-orm with the following query:
Account.filter(wallet=wallet).annotate(max_index=Max("account_index")).order_by('-account_index').first()
The schema is as follows:
Account.py
class Account(Model):
wallet = fields.ForeignKeyField('db.Wallet', on_delete=fields.CASCADE, related_name='accounts', index=True)
address = fields.CharField(max_length=65)
account_index = fields.IntField()
work = fields.BooleanField(default=True)
created_at = fields.DatetimeField(auto_now_add=True)
class Meta:
table = 'accounts'
unique_together = ('address', 'account_index')
Wallet.py
class Wallet(Model):
id = fields.UUIDField(pk=True)
seed = fields.CharField(max_length=128, unique=True)
representative = fields.CharField(max_length=65, null=True)
encrypted = fields.BooleanField(default=False)
work = fields.BooleanField(default=True)
created_at = fields.DatetimeField(auto_now_add=True)
class Meta:
table = 'wallets'
The exception is as follows:
ERROR:aiohttp.server:Error handling request
Traceback (most recent call last):
File "/home/pippin/.local/lib/python3.6/site-packages/tortoise/backends/asyncpg/client.py", line 36, in translate_exceptions_
return await func(self, *args)
File "/home/pippin/.local/lib/python3.6/site-packages/tortoise/backends/asyncpg/client.py", line 172, in execute_query
rows = await connection.fetch(*params)
File "/home/pippin/.local/lib/python3.6/site-packages/asyncpg/connection.py", line 420, in fetch
return await self._execute(query, args, 0, timeout)
File "/home/pippin/.local/lib/python3.6/site-packages/asyncpg/connection.py", line 1403, in _execute
query, args, limit, timeout, return_status=return_status)
File "/home/pippin/.local/lib/python3.6/site-packages/asyncpg/connection.py", line 1411, in __execute
return await self._do_execute(query, executor, timeout)
File "/home/pippin/.local/lib/python3.6/site-packages/asyncpg/connection.py", line 1423, in _do_execute
stmt = await self._get_statement(query, None)
File "/home/pippin/.local/lib/python3.6/site-packages/asyncpg/connection.py", line 328, in _get_statement
statement = await self._protocol.prepare(stmt_name, query, timeout)
File "asyncpg/protocol/protocol.pyx", line 163, in prepare
asyncpg.exceptions.GroupingError: column "accounts.wallet_id" must appear in the GROUP BY clause or be used in an aggregate function
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/pippin/.local/lib/python3.6/site-packages/aiohttp/web_protocol.py", line 418, in start
resp = await task
File "/home/pippin/.local/lib/python3.6/site-packages/aiohttp/web_app.py", line 458, in _handle
resp = await handler(request)
File "/home/pippin/.local/lib/python3.6/site-packages/aiohttp/web_middlewares.py", line 119, in impl
return await handler(request)
File "/home/pippin/.local/lib/python3.6/site-packages/aiohttp/web_middlewares.py", line 109, in impl
return await handler(request)
File "/home/pippin/.local/lib/python3.6/site-packages/pippin/server/pippin_server.py", line 69, in gateway
return await self.wallet_create(request, request_json)
File "/home/pippin/.local/lib/python3.6/site-packages/pippin/server/pippin_server.py", line 145, in wallet_create
await wallet.account_create(using_db=conn)
File "/home/pippin/.local/lib/python3.6/site-packages/pippin/db/models/wallet.py", line 144, in account_create
account = await self.get_newest_account()
File "/home/pippin/.local/lib/python3.6/site-packages/pippin/db/models/wallet.py", line 139, in get_newest_account
return await acct.Account.filter(wallet=self).annotate(max_index=Max("account_index")).order_by('-account_index').first()
File "/home/pippin/.local/lib/python3.6/site-packages/tortoise/queryset.py", line 613, in _execute
).execute_select(self.query, custom_fields=list(self._annotations.keys()))
File "/home/pippin/.local/lib/python3.6/site-packages/tortoise/backends/base/executor.py", line 105, in execute_select
_, raw_results = await self.db.execute_query(query.get_sql())
File "/home/pippin/.local/lib/python3.6/site-packages/tortoise/backends/asyncpg/client.py", line 38, in translate_exceptions_
raise OperationalError(exc)
tortoise.exceptions.OperationalError: column "accounts.wallet_id" must appear in the GROUP BY clause or be used in an aggregate function
Appears like some error generating the SQL with postgres (the same query works fine on SQLite).
Worth noting, that the same query works fine on older tortoise-versions (0.15.5 was the previous version used)
Thanks
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
must appear in the GROUP BY clause or be used in an ...
Yes, this is a common aggregation problem. Before SQL3 (1999), the selected fields must appear in the GROUP BY clause[*].
Read more >Postgres error [column must appear in the GROUP BY clause ...
ERROR : column "employee.empname" must appear in the GROUP BY clause or be used in an aggregate function The only difference between two...
Read more >U118: Column must appear in the GROUP BY clause
Explanation: This error occurs since you've specified an aggregate function (e.g. MAX(...) ) alongside a regular column in the query's target list, ...
Read more >asyncpg dialect's dollar-number conversion does not use the ...
GroupingError'>: column "alarms.attributes" must appear in the GROUP BY clause or be used in an aggregate function [SQL: SELECT ...
Read more >ERROR: column must appear in the GROUP BY clause or be ...
ERROR : column must appear in the GROUP BY clause or be used in an aggregate function. 3.2K views 3 years ago. Merche...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@grigi Tested the archive and confirmed it’s fixed, Thank you!
Released in 0.15.24 & 0.16.8