如何停止“ ValueError:tortoise.backends.sqlite.client.SqliteClient对象是在其他上下文中创建的”? [英] How do I stop "ValueError: tortoise.backends.sqlite.client.SqliteClient object was created in a different Context"?

查看:90
本文介绍了如何停止“ ValueError:tortoise.backends.sqlite.client.SqliteClient对象是在其他上下文中创建的”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个不和谐的机器人,并使用乌龟Orm来存储数据。我需要使用事务,但是我不断收到以下错误:

I'm writing a discord bot and using tortoise orm to store data. I need to use transactions, but I continually get the following error:

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] 

我正在创建我的交易,例如因此,在不协调的ext.commands命令中

I'm creating my transactions like so, in a discord ext.commands command

        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>在0x7f52ef362630>在0x7f52dd636c80>是在不同的上下文中创建的是什么意思,如何删除它?

I've also tried to start my transactions with a function, which results in the same error. My question is: what does 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 mean, and how do I get it to go away?

推荐答案

我已经解决了我的问题!
在遍历我的代码并查看可能造成的干扰时,我发现我正在使用 nest_asyncio ,我认为这不是问题

I've figured out my issue! While going through my code and seeing what could be interfering I found that I was using nest_asyncio, which I didn't believe to be an issue

已删除模块,代码现在可以正常工作

Having removed the module, the code now works

这篇关于如何停止“ ValueError:tortoise.backends.sqlite.client.SqliteClient对象是在其他上下文中创建的”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆