Search code examples
fastapialembicsqlmodel

Can not run migrations with alembic. Socket error


Here i created a User model using sqlmodel.

from sqlmodel import SQLModel, Field, Index
from typing import Optional

class User(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str = Field(max_length=30)
    surname: str = Field(max_length=50)

docker-compose with postgres:

version: "3.4"
services:
  db:
    image: postgres:14.0-alpine
    restart: always
    container_name: test_db
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=testdb
    ports:
    - "5432:5432"
    volumes:
    - db:/var/lib/postgresql/data
volumes:
  db:

Now i am trying to create migrations with alembic revision with "alembic revision --autogenerate -m "msg""

But it falls with

  File "C:\Python3.10\lib\socket.py", line 955, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

Solution

  • The error indicates that the hostname cannot be resolved. Is your database running locally? Has your python file that holds the SQLAlchemy engine access to it?

    I also see that you are running albemic from your global python installation (File "C:\Python3.10\), does that have the same dependencies as your python application? In any case, it is very advisable to use virtual environments to ensure you are developing with the same modules as you are running albemic migrations with.