0

我正在编写一个不和谐的机器人并使用乌龟 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意思,我如何让它消失?

4

1 回答 1

2

我已经弄清楚我的问题了!在查看我的代码并查看可能干扰的内容时,我发现我正在使用nest_asyncio,我认为这不是问题

删除模块后,代码现在可以工作了

于 2020-05-24T20:03:48.173 回答