Skip to content

Commit 91b20b1

Browse files
committed
fix: mysql query
1 parent c82a471 commit 91b20b1

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

app/jobs/record_usages.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,16 @@ async def record_user_stats(params: list[dict], node_id: int, usage_coefficient:
112112

113113
elif dialect == "mysql":
114114
# MySQL: INSERT ... ON DUPLICATE KEY UPDATE
115-
116115
stmt = mysql_insert(NodeUserUsage).values(
117116
user_id=bindparam("uid"),
118117
node_id=bindparam("node_id"),
119118
created_at=bindparam("created_at"),
120119
used_traffic=bindparam("value"),
121120
)
122-
stmt = stmt.on_duplicate_key_update(used_traffic=NodeUserUsage.used_traffic + bindparam("value"))
121+
# Use stmt.inserted to reference the inserted value (VALUES() in SQL)
122+
stmt = stmt.on_duplicate_key_update(
123+
used_traffic=NodeUserUsage.used_traffic + stmt.inserted.used_traffic
124+
)
123125
await safe_execute(db, stmt, upsert_params)
124126

125127
else: # SQLite
@@ -199,9 +201,10 @@ async def record_node_stats(params: list[dict], node_id: int):
199201
uplink=bindparam("up"),
200202
downlink=bindparam("down"),
201203
)
204+
# Use stmt.inserted to reference the inserted values (VALUES() in SQL)
202205
stmt = stmt.on_duplicate_key_update(
203-
uplink=NodeUsage.uplink + bindparam("up"),
204-
downlink=NodeUsage.downlink + bindparam("down"),
206+
uplink=NodeUsage.uplink + stmt.inserted.uplink,
207+
downlink=NodeUsage.downlink + stmt.inserted.downlink,
205208
)
206209
await safe_execute(db, stmt, [upsert_param])
207210

0 commit comments

Comments
 (0)