FastAPI-PgStarterKit icon indicating copy to clipboard operation
FastAPI-PgStarterKit copied to clipboard

What are the steps for adding new models / tbls?

Open alex-96-eng opened this issue 2 years ago • 5 comments

If I make model changes then run:

alembic revision --autogenerate -m "Added ..."

I get the following error:

    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not translate host name "db" to address: nodename nor servname provided, or not known

alex-96-eng avatar Sep 26 '23 06:09 alex-96-eng

I have a new file, which I have made sure is imported in db/base.py

alex-96-eng avatar Sep 26 '23 08:09 alex-96-eng

How do you rub the alembic @alex-96-eng? The error message shows that alembic wants to connect to a host called db, which does not exist. I think you are trying to run alembic directly on your dev machine. And there the db host does not exist. It only exists as a container within the docker-compose "cluster".

There are 2 solutions:

  • Make postgres accessible on localhost:

    • start only the db container with docker-compose after modifying the docker-compose file to have the container export port 5432 (the default postgres port)
    • run alembic locally, but have it go to localhost:5432 instead of to db.
  • Run it in the container:

    • start the "cluster" as usual. Do not touch your app.
    • connect to the container with docker exec -it <name of your app container> bash
    • run alembic in this container. It should find db now. You may have a problem with your app if it crashes before the migrations have been completed and kills the container with it. In that case you temporarily need to replace its start-up string with a sleep 10000 or so,

gmos avatar Sep 26 '23 13:09 gmos

Postgres (Use these later in your first pgadmin log-on)

POSTGRES_USER=postgres POSTGRES_PASSWORD=anihortes POSTGRES_SERVER=localhost # use localhost insted of db while using alembic POSTGRES_DB=postgres

this workaround may help for now

bibektimilsina000 avatar Sep 26 '23 13:09 bibektimilsina000

Oeps, the compose file already exports 5432 to localhost. So no need to modify it.

@bibektimilsina000, you could consider this to be a security bug. When running in the container cluster, both pgadmin and fastapi-app can access db directly on the virtual network that docker-compose creates. Exporting the port also to localhost is not required and makes the database more vulnerable than it needs to be.
But sometimes it is handy to have it exported...

gmos avatar Sep 26 '23 14:09 gmos

How do you rub the alembic @alex-96-eng? The error message shows that alembic wants to connect to a host called db, which does not exist. I think you are trying to run alembic directly on your dev machine. And there the db host does not exist. It only exists as a container within the docker-compose "cluster".

There are 2 solutions:

  • Make postgres accessible on localhost:

    • start only the db container with docker-compose after modifying the docker-compose file to have the container export port 5432 (the default postgres port)
    • run alembic locally, but have it go to localhost:5432 instead of to db.
  • Run it in the container:

    • start the "cluster" as usual. Do not touch your app.
    • connect to the container with docker exec -it <name of your app container> bash
    • run alembic in this container. It should find db now. You may have a problem with your app if it crashes before the migrations have been completed and kills the container with it. In that case you temporarily need to replace its start-up string with a sleep 10000 or so,

Thank you for this, it has unblocked me. I connected to the docker container, ran the script and it doesn't error. However, then when I added a new model, created a migration, the versions/ file is blank for the migration. Is it just me or is Alembic a little temperamental...

alex-96-eng avatar Sep 26 '23 23:09 alex-96-eng