tortoise/tortoise-orm

Usage with Tornado (was: No DB associated to model)

Open

#99 opened on Feb 3, 2019

View on GitHub
 (17 comments) (0 reactions) (0 assignees)Python (333 forks)batch import
documentationgood first issuepapercut

Repository metrics

Stars
 (3,863 stars)
PR merge metrics
 (Avg merge 15d 7h) (9 merged PRs in 30d)

Description

from tortoise.models import Model
from tortoise.fields import *

class Tournament(Model):
    id = IntField(pk=True)
    name = TextField()

if __name__ == "__main__":
    from tortoise import Tortoise, run_async


    async def init():
        # Here we create a SQLite DB using file "db.sqlite3"
        #  also specify the app name of "models"
        #  which contain models from "app.models"
        await Tortoise.init(
            db_url='sqlite://otc.db',
            modules={'models': ['model']} # 传入模块名称,本模块名为 model
        )
        # Generate the schema
        await Tortoise.generate_schemas()


    run_async(init())

    async def dps():
        await Tournament.create(name='Another Tournament')

        # Now search for a record
        tour = await Tournament.filter(name__contains='Another').first()
        print(tour.name)
    run_async(dps())

============================== tortoise.exceptions.ConfigurationError: No DB associated to model

WHY ?

Contributor guide