Fix “invalid literal for int() with base 10: 'None'” in FastAPI (Asyncpg + Docker)
You're using Docker to deploy your FastAPI application with Aiven PostgreSQL, when all of a sudden you notice this traceback: ValueError: invalid literal for int() with base 10: 'None' It refers to...

Source: DEV Community
You're using Docker to deploy your FastAPI application with Aiven PostgreSQL, when all of a sudden you notice this traceback: ValueError: invalid literal for int() with base 10: 'None' It refers to SQLAlchemy's URL parser and complains that the string "-None" is the port portion of your database URL. Your URL appears as postgresql+asyncpg://user:pass@host:None/dbname. What caused that to occur? And above all, how do you fix it? In this tutorial, I'll walk you through the cause of this error and provide a clean solution. We'll also discuss best practices for managing environment variables in containerized FastAPI applications. Understanding the Error The error occurs when SQLAlchemy's make_url() function tries to parse the database URL and convert the port component to an integer, but receives the literal string "None". This happens because somewhere in your code, you're building the URL with a variable that evaluates to None, and when interpolated into an f-string, None becomes the str