Jinto Lonappan
Jinto Lonappan

Reputation: 332

SQLAlchemy Alembic: Issue with URL for SQLServer

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.

Upvotes: 2

Views: 3743

Answers (2)

Jinto Lonappan
Jinto Lonappan

Reputation: 332

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

Upvotes: 2

Miguel Grinberg
Miguel Grinberg

Reputation: 67492

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.

Upvotes: 1

Related Questions