Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What are the steps for adding new models / tbls? #14

Open
alex-96-eng opened this issue Sep 26, 2023 · 5 comments
Open

What are the steps for adding new models / tbls? #14

alex-96-eng opened this issue Sep 26, 2023 · 5 comments

Comments

@alex-96-eng
Copy link

alex-96-eng commented Sep 26, 2023

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
Copy link
Author

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

@gmos
Copy link
Contributor

gmos commented Sep 26, 2023

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,

@bibektimilsina000
Copy link
Owner

bibektimilsina000 commented Sep 26, 2023

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

@gmos
Copy link
Contributor

gmos commented Sep 26, 2023

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...

@alex-96-eng
Copy link
Author

alex-96-eng commented Sep 26, 2023

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants