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]
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)
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?
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