Skip to content

Commit fb2a3a8

Browse files
M03EDImMohammad20000
authored andcommitted
fix(node): group by in postgresql
1 parent 868ffb8 commit fb2a3a8

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

app/node/user.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from PasarGuardNodeBridge import create_proxy, create_user
2-
from sqlalchemy import and_, func, select
2+
from sqlalchemy import Text, and_, cast, func, select
33

44
from app.db import AsyncSession
55
from app.db.models import Group, ProxyInbound, User, UserStatus, inbounds_groups_association, users_groups_association
@@ -28,12 +28,15 @@ def serialize_user_for_node(id: int, username: str, user_settings: dict, inbound
2828
async def core_users(db: AsyncSession):
2929
dialect = db.bind.dialect.name
3030

31-
# Use dialect-specific aggregation function
31+
# Use dialect-specific aggregation and grouping
3232
if dialect == "postgresql":
3333
inbound_agg = func.string_agg(ProxyInbound.tag.distinct(), ",").label("inbound_tags")
34+
# PostgreSQL can't GROUP BY JSON directly, cast to text
35+
proxy_settings_group = cast(User.proxy_settings, Text)
3436
else:
3537
# MySQL and SQLite use group_concat
3638
inbound_agg = func.group_concat(ProxyInbound.tag.distinct()).label("inbound_tags")
39+
proxy_settings_group = User.proxy_settings
3740

3841
stmt = (
3942
select(
@@ -53,7 +56,7 @@ async def core_users(db: AsyncSession):
5356
.outerjoin(inbounds_groups_association, Group.id == inbounds_groups_association.c.group_id)
5457
.outerjoin(ProxyInbound, inbounds_groups_association.c.inbound_id == ProxyInbound.id)
5558
.where(User.status.in_([UserStatus.active, UserStatus.on_hold]))
56-
.group_by(User.id, User.username, User.proxy_settings)
59+
.group_by(User.id, User.username, proxy_settings_group)
5760
)
5861

5962
results = (await db.execute(stmt)).all()

0 commit comments

Comments
 (0)