Skip to content

Commit 245b8dc

Browse files
Revert "fix: 修复 DetachedInstanceError (#69)" (#70)
This reverts commit dc917ca.
1 parent dc917ca commit 245b8dc

File tree

6 files changed

+33
-19
lines changed

6 files changed

+33
-19
lines changed

nonebot_plugin_value/api/api_balance.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ async def get_or_create_account(
9292
currency_id = (await _get_default()).id
9393
async with get_session() as session:
9494
data = await _go_account(user_id, currency_id, session)
95-
session.add(data)
9695
return UserAccountData.model_validate(data, from_attributes=True)
9796

9897

nonebot_plugin_value/repository.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
from nonebot import logger
77
from nonebot_plugin_orm import AsyncSession
8-
from sqlalchemy import delete, select, update
9-
from sqlalchemy.orm import joinedload
8+
from sqlalchemy import delete, insert, select, update
109

1110
from .exception import (
1211
AccountFrozen,
@@ -41,19 +40,18 @@ async def get_or_create_currency(
4140
)
4241
)
4342
if (currency := stmt.scalars().first()) is not None:
44-
session.add(currency)
4543
return currency, True
46-
result = await self.createcurrency(currency_data)
44+
await self.createcurrency(currency_data)
45+
result = await self.get_currency(currency_data.id)
46+
assert result is not None
4747
return result, False
4848

49-
async def createcurrency(self, currency_data: CurrencyData) -> CurrencyMeta:
49+
async def createcurrency(self, currency_data: CurrencyData):
5050
async with self.session as session:
5151
"""创建新货币"""
52-
currency = CurrencyMeta(**currency_data.model_dump())
53-
session.add(currency)
52+
stmt = insert(CurrencyMeta).values(**dict(currency_data))
53+
await session.execute(stmt)
5454
await session.commit()
55-
await session.refresh(currency)
56-
return currency
5755

5856
async def update_currency(self, currency_data: CurrencyData) -> CurrencyMeta:
5957
"""更新货币信息"""
@@ -144,7 +142,6 @@ async def get_or_create_account(
144142
# 检查账户是否存在
145143
stmt = (
146144
select(UserAccount)
147-
.options(joinedload(UserAccount.currency_id))
148145
.where(UserAccount.uni_id == get_uni_id(user_id, currency_id))
149146
.with_for_update()
150147
)
@@ -165,7 +162,6 @@ async def get_or_create_account(
165162
)
166163
session.add(account)
167164
await session.commit()
168-
await session.refresh(account)
169165
return account
170166
except Exception:
171167
await session.rollback()
@@ -349,9 +345,18 @@ async def create_transaction(
349345
)
350346
session.add(transaction_data)
351347
await session.commit()
352-
await session.refresh(transaction_data)
353-
session.add(transaction_data)
354-
return transaction_data
348+
stmt = (
349+
select(Transaction)
350+
.where(
351+
Transaction.id == uuid,
352+
)
353+
.with_for_update()
354+
)
355+
result = await session.execute(stmt)
356+
transaction = result.scalar_one_or_none()
357+
assert transaction, f"无法读取到交易记录[... WHERE id = {uuid} ...]!"
358+
session.add(transaction)
359+
return transaction
355360

356361
async def get_transaction_history(
357362
self, account_id: str, limit: int = 100

nonebot_plugin_value/services/balance.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ async def batch_del_balance(
149149

150150
if not all(r.success for r in result_list):
151151
return [] if not return_all_on_fail else result_list
152+
await session.commit()
152153
return result_list
153154

154155

@@ -260,6 +261,7 @@ async def batch_add_balance(
260261
result_list.append(data)
261262
if not all(r.success for r in result_list):
262263
return [] if not return_all_on_fail else result_list
264+
await session.commit()
263265
return result_list
264266

265267

@@ -321,12 +323,15 @@ async def add_balance(
321323
balance_before,
322324
balance_after,
323325
)
324-
await session.refresh(account)
326+
# 在更新余额前重新获取账户对象以避免DetachedInstanceError
327+
updated_account = await account_repo.get_or_create_account(user_id, currency_id)
325328
await account_repo.update_balance(
326-
account.id,
329+
updated_account.id,
327330
balance_after,
328331
currency_id,
329332
)
333+
if arg_session is None:
334+
await session.commit()
330335
try:
331336
await HooksManager().run_hooks(
332337
HooksType.post(),
@@ -452,6 +457,10 @@ async def transfer_funds(
452457
balance_after=to_balance_after,
453458
timestamp=timestamp,
454459
)
460+
461+
# 提交事务
462+
if arg_session is None:
463+
await session.commit()
455464
try:
456465
await HooksManager().run_hooks(
457466
HooksType.post(),

nonebot_plugin_value/services/transaction.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ async def remove_transaction(
7272
await TransactionRepository(session).remove_transaction(transaction_id)
7373
return True
7474
except Exception:
75+
await session.rollback()
7576
if fail_then_throw:
7677
raise
7778
return False

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "nonebot-plugin-value"
3-
version = "0.1.3.post2"
3+
version = "0.1.3.post1"
44
description = "Economy API for NoneBot2"
55
readme = "README.md"
66
requires-python = ">=3.10, <4.0.0"

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)