Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tortoise orm cannot work when I upgrade the version of nicegui to 1.4.x #1901

Closed
hu76589 opened this issue Oct 27, 2023 · 8 comments
Closed
Labels
documentation Improvements or additions to documentation
Milestone

Comments

@hu76589
Copy link

hu76589 commented Oct 27, 2023

Description

with nicegui 1.3.18,it works well:

# models.py
from tortoise import fields, models

class Subject(models.Model):
    subject_id = fields.IntField(pk=True)
    subject_pid = fields.IntField()
    subject_code = fields.CharField(max_length=50)
    subject_name = fields.CharField(max_length=50)
    subject_dc = fields.CharField(max_length=1)
    is_expenses = fields.IntField()
    is_using = fields.IntField()
    is_query = fields.IntField()
    original_balance = fields.DecimalField(10, 2)
# action.py
from tortoise.contrib.fastapi import register_tortoise
from nicegui import app

import models

register_tortoise(
    app,
    db_url='sqlite://books.sqlite3',
    modules={'models': ['models']},
    generate_schemas=False,
)

async def get_using_subjects_tree():
    # This line of code reported an error
    subjects = await models.Subject.filter(is_using = 1).order_by('subject_code')
    # do somethings...

but with 1.4.x, it cannot work, the error is:

default_connection for the model <class 'models.Subject'> cannot be None
@falkoschindler
Copy link
Contributor

Oh dear, it looks like tortoise isn't compatible with the new FastAPI lifespan API: tortoise/tortoise-orm#1371. We upgraded in #1849 because the old API is deprecated. But other libraries still seem to be using it.

Maybe we can add support for both, FastAPI below 0.95.0 and above?

@hu76589
Copy link
Author

hu76589 commented Oct 27, 2023

So it seems like we need to wait for tortoise to upgrade?

@falkoschindler
Copy link
Contributor

I found a workaround:

async def init_db() -> None:
await Tortoise.init(db_url='sqlite://db.sqlite3', modules={'models': ['models']})
await Tortoise.generate_schemas()
async def close_db() -> None:
await Tortoise.close_connections()
app.on_startup(init_db)
app.on_shutdown(close_db)

This way we don't have to wait for tortoise/tortoise-orm#1371. 🙂

@falkoschindler falkoschindler added this to the 1.4.2 milestone Oct 27, 2023
@falkoschindler falkoschindler added the documentation Improvements or additions to documentation label Oct 27, 2023
@falkoschindler
Copy link
Contributor

Oh no, the solution causes an exception when closing the app on Python 3.11:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.11/site-packages/aiosqlite/core.py", line 109, in run
    get_loop(future).call_soon_threadsafe(set_result, future, result)
  File "uvloop/loop.pyx", line 1288, in uvloop.loop.Loop.call_soon_threadsafe
  File "uvloop/loop.pyx", line 671, in uvloop.loop.Loop._append_ready_handle
  File "uvloop/loop.pyx", line 703, in uvloop.loop.Loop._check_closed
RuntimeError: Event loop is closed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/homebrew/Cellar/[email protected]/3.11.6/Frameworks/Python.framework/Versions/3.11/lib/python3.11/threading.py", line 1045, in _bootstrap_inner
    self.run()
  File "/opt/homebrew/lib/python3.11/site-packages/aiosqlite/core.py", line 117, in run
    get_loop(future).call_soon_threadsafe(set_exception, future, e)
  File "uvloop/loop.pyx", line 1288, in uvloop.loop.Loop.call_soon_threadsafe
  File "uvloop/loop.pyx", line 671, in uvloop.loop.Loop._append_ready_handle
  File "uvloop/loop.pyx", line 703, in uvloop.loop.Loop._check_closed
RuntimeError: Event loop is closed

This lets our startup test fail.

@falkoschindler
Copy link
Contributor

There is a related issue on aiosqlite: omnilib/aiosqlite#241
I think we need to ignore this startup test for now, wait for the issue to be fixed upstream and re-activate it later.

@rodja
Copy link
Member

rodja commented Oct 28, 2023

I'm fine with that. I've just tried to reproduce the problem locally. In most cases the demo exits normally. Maybe in 1 out of 10 cases I get the error.

@falkoschindler
Copy link
Contributor

Strange, it happens all the time on my machine.
Anyway, I just pushed a commit skipping this test on Python 3.11. Let's see if it passes.

@falkoschindler
Copy link
Contributor

After identifying tortoise/tortoise-orm#1371 and omnilib/aiosqlite#241 as root causes for existing problems with Tortoise, I'll close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants