1

I was trying to create a database migration script with Alembic for my SQLServer database and having issues with the connection string.

This is what I have now:

sqlalchemy.url = "mssql+pyodbc://db_server/database?trusted_connection=yes&driver=ODBC Driver 17 for SQL Server"

Error message during alembic current: sqlalchemy.exc.ArgumentError: Could not parse rfc1738 URL from string '"mssql+pyodbc://dbserverxx/dbxx?trusted_connection=yes&driver=ODBC Driver 17 for SQL Server"'

I tested my script with SQLite and is working fine.

I'm using a Mac and the contents of odbcinst.ini is as follows:

[ODBC Driver 17 for SQL Server]
Description=Microsoft ODBC Driver 17 for SQL Server
Driver=/usr/local/lib/libmsodbcsql.17.dylib
UsageCount=2

I was able to connect to database using PyODBC and execute raw SQLs.

Any suggestions to have the right sqlalchemy.url is appreciated.

Jinto Lonappan
  • 312
  • 2
  • 8

2 Answers2

1

I was able to resolve the issue by removing the quotes.

This is how my alembic.ini has the corresponding line now:

sqlalchemy.url = mssql+pyodbc://dbserver/database?trusted_connection=yes&driver=ODBC Driver 17 for SQL Server
Jinto Lonappan
  • 312
  • 2
  • 8
0

URLs cannot have spaces. Replace the spaces with + characters:

sqlalchemy.url = "mssql+pyodbc://db_server/database?trusted_connection=yes&driver=ODBC+Driver+17+for+SQL+Server"

You can learn more about URL encoding, spaces aren't the only characters that have to be escaped in URLs.

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
  • I tried this and didnt work. However, I was able to resolve it by removing the `"` (quotes) – Jinto Lonappan Jan 04 '21 at 23:35
  • Yes, a SQLAlchemy connection URI *should* be encoded, but spaces are actually not the problem (as the other answer shows). AFAIK the only characters which *must* be escaped are `:`, `@`, `/`, and `?` (and maybe `+` itself, not sure about that one). – Gord Thompson Jan 04 '21 at 23:40