英文:
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=".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
答案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 = ['lom.models']
- 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论