miguelgrinberg / retrofun

Code from my "SQLAlchemy 2 In Practice" book.
https://amzn.to/3S0diwc
MIT License
92 stars 16 forks source link

Alembic raising an error when creating the migration script for a postgreSQL DB #3

Open afcristia opened 6 months ago

afcristia commented 6 months ago

I am working through the book (Kindle version) and got stuck when introducing Alembic as the create migration command raises an error as follows:

File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/configparser.py", line 374, in before_set raise ValueError("invalid interpolation syntax in %r at " ValueError: invalid interpolation syntax in 'postgresql+psycopg2://retrofun:123.%2C@192.168.1.186:5432/retrofun' at position 35

I'm running a postgreSQL database on another computer on my local network. I tried copying the line used for SQLAlchemy to connect with no luck.

I know I could be missing something but I could find a suitable solution when googling for a solution, hence the need to reach out to you.

I am adding the env.py (as a .txt file as GitHub doesn't allow .py files)

env.txt

miguelgrinberg commented 6 months ago

@afcristia The "%" that you have in your password is causing this, because this character is used for interpolation. Use "%%" to escape it.

afcristia commented 6 months ago

thanks for your prompt reply. However, I'am affraid that's not it, as my password doesn't have a % sign.
env.txt

I'm attaching my .env file (with a .txt extension) so you can check. Alembic is changing ".," in my password for ".%2C" somewhere with its code, but I have no idea where it could be.

miguelgrinberg commented 6 months ago

To determine where is this happening it would be useful to see the complete stack trace of the error.

afcristia commented 6 months ago

error.txt

I'm now attaching the whole output. Thanks again for your support.

miguelgrinberg commented 6 months ago

In env.py, change this line:

config.set_main_option("sqlalchemy.url", engine.url.render_as_string(
    hide_password=False))

to this:

config.set_main_option("sqlalchemy.url", engine.url.render_as_string(
    hide_password=False).replace('%', '%%'))

Let me know if that helps!

afcristia commented 6 months ago

I just tried it and worked perfectly.
retrofun - public

Thanks so much.