tortoise / aerich

A database migrations tool for TortoiseORM, ready to production.
https://github.com/tortoise/aerich
Apache License 2.0
804 stars 90 forks source link

stuck on init #324

Closed xalteropsx closed 8 months ago

xalteropsx commented 8 months ago
raise ConfigurationError(

tortoise.exceptions.ConfigurationError: DB configuration not initialised. Make sure to call Tortoise.init with a valid configuration before attempting to create connections. raise ConfigurationError( tortoise.exceptions.ConfigurationError: DB configuration not initialised. Make sure to call Tortoise.init with a valid configuration before attempting to create connections. raise ConfigurationError( tortoise.exceptions.ConfigurationError: DB configuration not initialised. Make sure to call Tortoise.init with a valid configuration before attempting to create connections.

neonarc4 commented 8 months ago

@long2ice do u even test ur own module in new environment before pushing unwanted update making whole module fail image

neonarc4 commented 8 months ago

@long2ice at ur aerich-0.7.1 i face this bug while at ur aerich-0.7.2 i also face this sure to call Tortoise.ini bug image

xalteropsx commented 8 months ago

@YAGregor just feeling like to delete this tortoise the more i getting the more simple solution look a complex question and the community here no answer left question days and nothing work as what it mention

very frustrated i just plz help

YAGregor commented 8 months ago

I think, at lease you should provide minimal reproduce code, I use Aerich and it work well most of the time

xalteropsx commented 8 months ago

@YAGregor

this is my models.py


from tortoise.models import Model
from tortoise import fields

class Author(Model):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=100)

class Book(Model):
    id = fields.IntField(pk=True)
    title = fields.CharField(max_length=200)
    author = fields.ForeignKeyField('models.Author', related_name='books')
xalteropsx commented 8 months ago

# from tortoise import fields, models
from tortoise.contrib.pydantic import pydantic_model_creator
from tortoise import Tortoise, run_async
import asyncio
from typing import Optional, Any, List
from pydantic import BaseModel, ConfigDict
from models import Book, Author

db = 'postgres://postgres:480@localhost:5432/mytest'

# copy this and create app.py to test aerich failiure 1- aerich init -t filename.TORTOISE_ORM  2- aerich init-db

# aerich v0.7.2 fail this is failing ortoise.exceptions.ConfigurationError: DB configuration not initialised. Make sure to call Tortoise.init with a valid configuration before attempting to create connections.
# aerich 0.7.1 fail no model with name relationship failing
TORTOISE_ORM = {
    "connections": {"default": db},
    "apps": {
        "contact": {
            "models": ["aerich.models", "models"],
            "default_connection": "default",
        },
    },
}

async def init():
    await Tortoise.init(db_url=db, modules={"models": ["__main__"]})
    await Tortoise.generate_schemas()

Author_PY = pydantic_model_creator(Author, name="Author")
Book_PY = pydantic_model_creator(Book, name="Book")

async def create_tournament(name):
    author = Author(name=name)
    await author.save()
    for i in range(2):
        await Book.create(title=f"participants_{i}", author=author)

    authors = await Author.get(id=1)

    store = dict(authors)
    print(store) 

if __name__ == "__main__":
    run_async(init())
    asyncio.run(create_tournament("New Tournament"))
YAGregor commented 8 months ago

At first you should never use generate_schemas with aerich, they are conflict

YAGregor commented 8 months ago

I found two mistakes in your code, you should never use generate_schemas, and you should write

class Book(Model):
    id = fields.IntField(pk=True)
    title = fields.CharField(max_length=200)
    # author = fields.ForeignKeyField('models.Author', related_name='books')
    author = fields.ForeignKeyField('contact.Author', related_name='books')  #  app_name.model not python module_name.model

then I delete migration folder and init again, it works well. tortoise and aerich's error message is just too unfriendly and hard to figure out what's wrong, and it's way config is a bit hard to understand (in most cases we dont need multiple dbs or multiple app, but we must understand it for simplest config)

xalteropsx commented 8 months ago

@YAGregor



import os

current_file_name = os.path.basename(__file__)
current_file_name, extension = os.path.splitext(current_file_name)

def realtionship(data):
    data = f"{current_file_name}.{data}"
    print(data)
    return data

realtionship("Book")

why they dont do like this ? so it avoid making appname.
xalteropsx commented 8 months ago

I found two mistakes in your code, you should never use generate_schemas, and you should write

i not use generate same time if i use aerich or alembic just by mistake forget to comment thnx >.< i didnt aware contact was nested like that wish their docs little more friendly

YAGregor commented 8 months ago

@YAGregor

import os

current_file_name = os.path.basename(__file__)
current_file_name, extension = os.path.splitext(current_file_name)

def realtionship(data):
    data = f"{current_file_name}.{data}"
    print(data)
    return data

realtionship("Book")

why they dont do like this ? so it avoid making appname.

How do I know?Might just imitate design django, but django have a more explicit app.

xalteropsx commented 8 months ago

hmm guess it should be fine since it using multiple entity of db and models

tomhamiltonstubber commented 7 months ago

For anyone coming to this with the same error tortoise.exceptions.ConfigurationError: DB configuration not initialised. Make sure to call Tortoise.init with a valid configuration before attempting to create connections., you need to downgrade to 0.7.1