python - 如何停止“ValueError:tortoise.backends.sqlite.client.SqliteClient 对象是在不同的上下文中创建的”?
问题描述
我正在编写一个不和谐的机器人并使用乌龟 orm 来存储数据。我需要使用事务,但我不断收到以下错误:
Full type of the error is <class 'discord.ext.commands.errors.CommandInvokeError'>
Command raised an exception: ValueError: <Token var=<ContextVar name='default' default=<tortoise.backends.sqlite.client.SqliteClient object at 0x7f52ef5080d0> at 0x7f52ef362630> at 0x7f52dd636c80> was created in a different Context
- [x] Traceback (most recent call last):
- [x] File "/home/minion/PycharmProjects/Gone-Pair-Shaped/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 83, in wrapped
- [x] ret = await coro(*args, **kwargs)
- [x] File "/home/minion/PycharmProjects/CAHRewrite/cogs/terms/__init__.py", line 137, in agree
- [x] await guild.save(using_db=guild_creation)
- [x] File "/home/minion/PycharmProjects/Gone-Pair-Shaped/venv/lib/python3.8/site-packages/tortoise/backends/base/client.py", line 233, in __aexit__
- [x] current_transaction_map[self.connection_name].reset(self.token)
- [x] ValueError: <Token var=<ContextVar name='default' default=<tortoise.backends.sqlite.client.SqliteClient object at 0x7f52ef5080d0> at 0x7f52ef362630> at 0x7f52dd636c80> was created in a different Context
- [x]
- [x] The above exception was the direct cause of the following exception:
- [x]
- [x] Traceback (most recent call last):
- [x] File "/home/minion/PycharmProjects/Gone-Pair-Shaped/venv/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 892, in invoke
- [x] await ctx.command.invoke(ctx)
- [x] File "/home/minion/PycharmProjects/Gone-Pair-Shaped/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 797, in invoke
- [x] await injected(*ctx.args, **ctx.kwargs)
- [x] File "/home/minion/PycharmProjects/Gone-Pair-Shaped/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 92, in wrapped
- [x] raise CommandInvokeError(exc) from exc
- [x] discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ValueError: <Token var=<ContextVar name='default' default=<tortoise.backends.sqlite.client.SqliteClient object at 0x7f52ef5080d0> at 0x7f52ef362630> at 0x7f52dd636c80> was created in a different Context
- [x]
我正在创建我的交易,在 discord ext.commands 命令中
async with tortoise.transactions.in_transaction() as guild_creation:
default_game_settings = database.guild.GameDefaults()
await default_game_settings.save(using_db=guild_creation)
settings = database.guild.GuildSettings()
await settings.save(using_db=guild_creation)
guild = database.guild.Guild(
guild_id=ctx.guild.id,
agreed_at=datetime.datetime.now(),
agreed_by=ctx.author.id,
default_settings=default_game_settings,
settings=settings,
)
await guild.save(using_db=guild_creation)
我还尝试使用函数开始我的事务,这会导致相同的错误。我的问题是:这是什么ValueError: <Token var=<ContextVar name='default' default=<tortoise.backends.sqlite.client.SqliteClient object at 0x7f52ef5080d0> at 0x7f52ef362630> at 0x7f52dd636c80> was created in a different Context
意思,我如何让它消失?
解决方案
我已经弄清楚我的问题了!在查看我的代码并查看可能干扰的内容时,我发现我正在使用nest_asyncio
,我认为这不是问题
删除模块后,代码现在可以工作了
推荐阅读
- javascript - 如何在 3s 视频中的特定帧处执行 DOM 操作?
- python - 无法将项目添加到新的 DynamoDB 表
- android - 想在不同的类文件中调用其他活动
- java - 添加 Firebase Admin 6.12.2 后 Netty 无法处理请求
- kotlin - 使用 listOf 与 arrayListOf
- python - 尝试在远程主机中安装 git 时出现 AnsibleAuthenticationFailure
- java - 演示文稿被取消,因为显示指标自创建以来已更改
- regex - 匹配正则表达式中确切数量的标记
- reactjs - 无法在 shopify react 应用程序中注册已卸载的网络挂钩
- reactjs - 如何隐藏 gatsby 主题的文本内容?