pythonfastapitortoise-orm

How to create db migrations for local tortoise project?


I have a FastAPI + tortose projects and I want to run the project locally with database postgres://lom:lom@localhost:5432/lom (database is created)

My code

# lom/app.py
class App:
    storage: S3Storage

    def __init__(self):
        self.config = Config(_env_file=".env", _env_file_encoding="utf-8")
        self.__setup_sentry()
        ...
    def create_app(self, loop: asyncio.AbstractEventLoop) -> FastAPI:
        app = FastAPI()
        register_tortoise(
            app,
            modules={
                "models": [
                    "lom.core",
                    "aerich.models",
                ]
            }

I want to apply current migrations and create new migrations I am trying

aerich init -t <Don't understand path>

What aerich command should I run and which parameters should I use?

├── lom
│   ├── app.py
│   ├── config.py
│   ├── core
│   │   ├── city.py
│   │   ├── company.py
├── 
├── migrations
│   ├── 001_main.sql
│   ├── 002_cities.sql
│   ├── 003_cities_declination.sql

Solution

  • This will create an aerich.ini configuration file and a migrations directory in the root directory of your project.

    [tortoise]

    # database_url = sqlite://db.sqlite3

    database = lom

    host = localhost

    port = 5432

    user = lom

    password = lom

    modules = ['lom.models']

    This will create a new migration file in the migrations directory.

    This will apply all pending migrations to your database and the problem you're facing will be solved. Let me know if this helps