FastAPI Alembic迁移文件并没有被创建,即使输出显示是这样。文件夹“版本”为空,数据库中缺少表

2024-04-29 09:51:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将Postgresql合并到我的FastAPI中。经过研究,我决定遵循本教程-https://youtu.be/NH4VZaP3_9s 完整的代码可以在这里找到-https://github.com/veryacademy/YT_FastAPI_Beginner_Fast-Track

我检查了几次,并将我的代码与教程1进行了比较,没有发现任何差异

但当我运行命令时

docker-compose run my_api alembic revision --autogenerate -m "New Migration"

我得到了这个输出:

Creating my_api_my_api_run ... done
INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
INFO  [alembic.autogenerate.compare] Detected added table 'user'
INFO  [alembic.autogenerate.compare] Detected added index 'ix_user_id' on '['id']'
INFO  [alembic.autogenerate.compare] Detected added table 'captcha_solve_query'
INFO  [alembic.autogenerate.compare] Detected added index 'ix_captcha_solve_query_id' on '['id']'
INFO  [alembic.autogenerate.compare] Detected added table 'credits_transaction'
INFO  [alembic.autogenerate.compare] Detected added index 'ix_credits_transaction_id' on '['id']'
  Generating /my_api/alembic/versions/858d5482faf8_new_migration.py ...  done

但是我的文件夹versions仍然是空的。我试图在我的mac上搜索这个文件858d5482faf8_new_migration.py,但它没有返回任何内容

不管怎样,我试着继续,然后执行了下一步:

docker-compose run my_api alembic upgrade head

我收到了这个输出:

Creating my_api_my_api_run ... done
INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.

这开始有点奇怪,因为在本教程中,输出有类似以下内容的附加行:

Running upgrade -> 858d5482faf8 New, Migration

我错过了这一行,但仍然没有错误

之后,当我手动检查postgres或尝试执行请求时,我会看到/得到丢失表的错误,就像我不会执行任何迁移一样

我不确定发生了什么,因为我遵循了教程,我没有看到任何错误。 (我只得到关于丢失CSRF令牌的错误,但我在教程中也看到了,所以我猜不是这样的?)

有人能给我指一下正确的方向吗

| | | | | | | | | | | | | | | | | | |编辑| | | | |

我改变了一些东西,并且有点(?)设法把它推进了一步(?)。上面的命令仍然适用,但这一次在启动docker后出现以下错误:

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused Is the server running on host "postgresql_db" (192.168.80.2) and accepting TCP/IP connections on port 5432?

这是我的docker-compose.yml:

version: "3.8"

services:
  database:
    container_name: postgresql_db
    image: postgres
    restart: always
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=${DB_USER}
      - POSTGRES_PASSWORD=${DB_PASSWORD}
      - POSTGRES_DB=${DB_NAME}

  pgadmin:
    container_name: pgadmin
    image: dpage/pgadmin4
    environment:
      - PGADMIN_DEFAULT_EMAIL=${PGADMIN_EMAIL}
      - PGADMIN_DEFAULT_PASSWORD=${PGADMIN_PASSWORD}
    ports:
      - "5050:80"
    depends_on:
      - database

  my_api:
    container_name: my_api
    build: .
    command: bash -c "alembic upgrade head && uvicorn main:app --host 0.0.0.0 --port 8000 --reload"
    volumes:
      - .:/app
    ports:
      - "8000:8000"
    depends_on:
      - database
    restart: always

这是我的.env:

DATABASE_URL=postgresql+psycopg2://postgres:password@postgresql_db:5432/my_api_db
DB_USER=postgres
DB_PASSWORD=password
DB_NAME=my_api_db
PGADMIN_EMAIL=admin@admin.com
PGADMIN_PASSWORD=admin

我在谷歌上搜索过它,常见的错误是使用localhost而不是容器的名称(我想这不是我),并在home address上指定您的postgresql地址(我想也不是我)


Tags: infoapiidaddeddbonmy错误