如何为本地Tortoise项目创建数据库迁移?

huangapple go评论69阅读模式
英文:

How to create db migrations for local tortoise project?

问题

以下是您要翻译的代码部分:

我有一个FastAPI + tortose项目我想在本地运行该项目数据库使用 `postgres://lom:lom@localhost:5432/lom`(数据库已创建

我的代码

    # 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",
                    ]
                }

我想应用当前的迁移并创建新的迁移
我正在尝试

    aerich init -t <不理解的路径>

我应该运行哪个aerich命令以及应该使用哪些参数

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

请注意,我已将代码部分翻译为中文。

英文:

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=&quot;.env&quot;, _env_file_encoding=&quot;utf-8&quot;)
        self.__setup_sentry()
        ...
    def create_app(self, loop: asyncio.AbstractEventLoop) -&gt; FastAPI:
        app = FastAPI()
        register_tortoise(
            app,
            modules={
                &quot;models&quot;: [
                    &quot;lom.core&quot;,
                    &quot;aerich.models&quot;,
                ]
            }

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

aerich init -t &lt;Don&#39;t understand path&gt;

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

答案1

得分: 4

  • 在项目的根目录中运行以下命令来初始化 aerich:
    aerich init -t tortoise.Tortoise -p lom.models

这将在项目的根目录中创建一个 aerich.ini 配置文件和一个 migrations 目录。

  • 配置 aerich.ini 文件以匹配你的数据库连接设置。默认设置是用于 SQLite 数据库的,所以你需要修改 [tortoise] 部分以使用 PostgreSQL。

[tortoise]

# database_url = sqlite://db.sqlite3

database = lom

host = localhost

port = 5432

user = lom

password = lom

modules = ['lom.models']

  • 运行以下命令来创建新的迁移:
    aerich migrate

这将在 migrations 目录中创建一个新的迁移文件。

  • 运行以下命令来应用迁移:
    aerich upgrade

这将将所有待处理的迁移应用到你的数据库,解决你遇到的问题。如果这有所帮助,请告诉我。

英文:
  • Initialize aerich by running the following command in the root directory of your project:
    aerich init -t tortoise.Tortoise -p lom.models

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

  • Configure the aerich.ini file to match your database connection settings. The default settings are for a SQLite database, so you will need to modify the [tortoise] section to use PostgreSQL instead.

[tortoise]

# database_url = sqlite://db.sqlite3

database = lom

host = localhost

port = 5432

user = lom

password = lom

modules = [&#39;lom.models&#39;]

  • Run the following command to create a new migration:
    aerich migrate

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

  • Run the following command to apply the migrations:
    aerich upgrade

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

huangapple
  • 本文由 发表于 2023年4月11日 08:45:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981677.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定