question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to create models

See original GitHub issue

Checklist

  • The bug is reproducible against the latest release and/or master.
  • There are no similar issues or pull requests to fix it yet.

Describe the bug

Unable to create models due to a RuntimeError caused by anyio.

To reproduce

database = databases.Database("url")
models = orm.ModelRegistry(database = database)
models.create_all()

Expected behavior

Models getting created. Successfully connecting to the database.

Actual behavior

The models don’t get created due to the RuntimeError being raised by anyio and it fails to connect to the database.

Debugging material

Traceback from ipython

    RuntimeError                         Traceback (most recent call last)
<ipython-input-1-3765cc235a53> in <module>
     16
     17 # Create the tables
---> 18 models.create_all()
     19
     20 await Note.objects.create(text="Buy the groceries.", completed=False)

/data/data/com.termux/files/usr/lib/python3.9/site-packages/orm/models.py in create_all(self)
     31     def create_all(self):
     32         url = self._get_database_url()
---> 33         anyio.run(self._create_all, url)
     34
     35     def drop_all(self):

/data/data/com.termux/files/usr/lib/python3.9/site-packages/anyio/_core/_eventloop.py in run(func, backend, backend_options, *args)
     40         pass
     41     else:
---> 42         raise RuntimeError(f'Already running {asynclib_name} in this thread')
     43
     44     try:

RuntimeError: Already running asyncio in this thread

Environment

  • OS: Linux aarch64 Android
  • Python version: 3.9.7
  • ORM version: 0.2.0

Additional context

I fixed it by making create_all an async function and using await self._create_all(url) instead of anyio.run(self._create_all, url) although I don’t think that’s the best way to fix this.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:38 (16 by maintainers)

github_iconTop GitHub Comments

1reaction
codecakescommented, Dec 4, 2021

First is that, you can still run your migrations independently, it’s just up to caller to set up the asyncio and cleanup when done in any script.

Ill come back with a pytest reproducible example on this point.

1reaction
aminalaeecommented, Nov 20, 2021

I think that misses a few imports and stuff, I could reproduce it with this example:

app.py

import databases
import orm
from fastapi import FastAPI

database = databases.Database("sqlite:///db.sqlite")

models = orm.ModelRegistry(database=database)


app = FastAPI()

@app.on_event("startup")
async def startup():
    models.create_all()

and running uvicorn app:app causes:

RuntimeError: Already running asyncio in this thread
Read more comments on GitHub >

github_iconTop Results From Across the Web

"We were unable to create your model due to technical issues ...
"We were unable to create your model due to technical issues" when creating a model using Model Builder in InfraWorks.
Read more >
Unable to create a model with Eloquent create method. Error ...
I have created a model named, Author. I tried creating a model with the help of eloquent create method like this:
Read more >
model.save() throwed out an OSError: Unable to create file ...
My workaround is to create a new callback which saves models/weights after each epoch with a new file name. Calling model.save() or model....
Read more >
Unable To Create A Data Model - Oracle Support
Unable To Create A Data Model (Doc ID 2123460.1) ... The below error occurs when creating a BI Data model via the Weblogic...
Read more >
Unable to create new Model entry - Odoo
I have tried to create a new model field with Odoo's UI. I Click on Settings->Developer Mode-> Technical-> Database Structure->Models and I try...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found