{"id":7642,"date":"2026-09-28T09:02:00","date_gmt":"2026-09-28T16:02:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/azure-sql\/?p=7642"},"modified":"2026-09-28T09:02:00","modified_gmt":"2026-09-28T16:02:00","slug":"sqlalchemy-2-1-brings-built-in-support-for-mssql-python","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/azure-sql\/sqlalchemy-2-1-brings-built-in-support-for-mssql-python\/","title":{"rendered":"SQLAlchemy 2.1 brings built-in support for mssql-python"},"content":{"rendered":"<p>We&#8217;re pleased to share that SQLAlchemy 2.1.0 is now generally available, with built-in support for <a href=\"https:\/\/github.com\/microsoft\/mssql-python\">mssql-python<\/a>, Microsoft&#8217;s Python driver for SQL Server. You can now use it with SQLAlchemy&#8217;s ORM and Core APIs through the first-party <code>mssql+mssqlpython<\/code> dialect.<\/p>\n<p>If SQLAlchemy is already part of how you build, we want this to feel like a natural next step &#8211; not another thing to learn. The goal is simple: help you connect to SQL Server and Azure SQL with less setup, while keeping the tools and patterns you know.<\/p>\n<p>This integration is now part of the production release, and SQLAlchemy 2.1 is the version installed by default when you run <code>pip install sqlalchemy<\/code>.<\/p>\n<h2>Key benefits<\/h2>\n<p>For SQLAlchemy users who connect to SQL Server or Azure SQL, the built-in mssql-python dialect provides:<\/p>\n<ul>\n<li><strong>Simpler setup and deployment:<\/strong> Install the driver as a Python package without separately installing and configuring an external ODBC driver.<\/li>\n<li><strong>A familiar development experience:<\/strong> Continue using SQLAlchemy ORM and Core features, including declarative models, sessions, transactions, connection pooling, and SQL expressions.<\/li>\n<li><strong>Built-in integration:<\/strong> Select mssql-python with the standard <code>mssql+mssqlpython<\/code> connection URL instead of relying on a third-party dialect or custom integration.<\/li>\n<li><strong>Modern authentication:<\/strong> Connect to Azure SQL with Microsoft Entra authentication, including managed identity scenarios that don&#8217;t require stored database passwords.<\/li>\n<li><strong>Straightforward adoption:<\/strong> Existing <code>mssql+pyodbc<\/code> applications can generally keep their SQLAlchemy models and query patterns while updating the driver dependency and connection URL.<\/li>\n<li><strong>Migration tooling:<\/strong> Continue using Alembic to manage database schema changes.<\/li>\n<\/ul>\n<p>You\u2019ve put time and care into your applications. Adopting a new driver should build on that work, not ask you to start over. This integration gives you a simpler path to SQL Server and Azure SQL while preserving the familiar SQLAlchemy programming model.<\/p>\n<h2 id=\"get-started\" class=\"atx\">Get started<\/h2>\n<p>SQLAlchemy 2.1 requires Python 3.11 or later.<\/p>\n<p>Install SQLAlchemy 2.1 and the mssql-python driver:<\/p>\n<pre><code class=\"fenced-code-block language-bash\">python -m pip install \"SQLAlchemy[mssql-python]&gt;=2.1.0\"<\/code><\/pre>\n<p>Note: If you have not previously used mssql-python, confirm you have your <a href=\"https:\/\/learn.microsoft.com\/en-sg\/sql\/connect\/python\/mssql-python\/installation?view=sql-server-ver17#platform-specific-notes\">platform-specific pre-requisites<\/a>.<\/p>\n<p>The mssql-python dialect uses <code>mssql+mssqlpython<\/code> as its connection URL scheme:<\/p>\n<pre><code class=\"fenced-code-block language-text\">mssql+mssqlpython:\/\/&lt;username&gt;:&lt;password&gt;@&lt;host&gt;:&lt;port&gt;\/&lt;database&gt;<\/code><\/pre>\n<p>Use <code>URL.create()<\/code> to build the URL without manually escaping special characters in credentials:<\/p>\n<pre><code class=\"fenced-code-block language-python\">\r\nfrom sqlalchemy import create_engine, text\r\nfrom sqlalchemy.engine import URL\r\n\r\nurl = URL.create(\r\n    \"mssql+mssqlpython\",\r\n    username=\"dbuser\",\r\n    password=\"&lt;password&gt;\",\r\n    host=\"&lt;server&gt;\",\r\n    port=1433,\r\n    database=\"&lt;database&gt;\",\r\n    query={\"encrypt\": \"yes\"},\r\n)\r\n\r\nengine = create_engine(url)\r\n\r\nwith engine.connect() as connection:\r\n    version = connection.execute(text(\"SELECT @@VERSION\")).scalar_one()\r\n    print(version)<\/code><\/pre>\n<p>After you create the engine, use it with the same SQLAlchemy ORM and Core APIs you already know.<\/p>\n<h2 id=\"connect-with-microsoft-entra-authentication\" class=\"atx\">Connect with Microsoft Entra authentication<\/h2>\n<p>For Azure SQL, you can specify a Microsoft Entra authentication method in the connection URL. For example, <code>ActiveDirectoryDefault<\/code> uses the Azure Identity credential chain:<\/p>\n<pre><code class=\"fenced-code-block language-python\">from sqlalchemy import create_engine, text\r\n\r\nengine = create_engine(\r\n    \"mssql+mssqlpython:\/\/@&lt;server&gt;.database.windows.net\/&lt;database&gt;\"\r\n    \"?authentication=ActiveDirectoryDefault&amp;encrypt=yes\"\r\n)\r\n\r\nwith engine.connect() as connection:\r\n    current_user = connection.execute(text(\"SELECT CURRENT_USER\")).scalar_one()\r\n    print(current_user)<\/code><\/pre>\n<p>Choose the Microsoft Entra authentication method that matches your environment. For production workloads hosted in Azure, managed identity can help you avoid storing database passwords in application configuration.<\/p>\n<h2 id=\"moving-from-sqlalchemy-20\" class=\"atx\">Moving from SQLAlchemy 2.0<\/h2>\n<p>The move from SQLAlchemy 2.0 to 2.1 is smaller than the earlier transition from 1.x to 2.0, but SQLAlchemy 2.1 includes behavioral changes that can affect existing applications. Before upgrading:<\/p>\n<ul>\n<li>Confirm that your application runs on Python 3.11 or later.<\/li>\n<li>Review the SQLAlchemy 2.1 migration guidance.<\/li>\n<li>Test your application&#8217;s queries, transactions, migrations, and connection-pool behavior.<\/li>\n<li>Pin dependency versions according to your deployment and update policy.<\/li>\n<\/ul>\n<p>If you&#8217;re already using <code>mssql+pyodbc<\/code>, adopting mssql-python requires changing the driver portion of the connection URL and validating the application against the new driver. You don&#8217;t need to rewrite your SQLAlchemy models or replace the ORM and Core patterns in your application.<\/p>\n<h2 id=\"learn-more\" class=\"atx\">Learn more<\/h2>\n<ul>\n<li>Follow the Microsoft Learn guide, <a href=\"https:\/\/learn.microsoft.com\/sql\/connect\/python\/mssql-python\/sqlalchemy-integration?view=sql-server-ver17\">Use mssql-python with SQLAlchemy<\/a>, for ORM and Core examples, connection pooling, web framework integration, Alembic migrations, and troubleshooting.<\/li>\n<li>Read the <a href=\"https:\/\/www.sqlalchemy.org\/blog\/2026\/09\/24\/sqlalchemy-2.1.0-released\/\">SQLAlchemy 2.1.0 release announcement<\/a> for highlights from the production release.<\/li>\n<li>Review <a href=\"https:\/\/docs.sqlalchemy.org\/en\/21\/changelog\/migration_21.html#microsoft-sql-server\">What&#8217;s New in SQLAlchemy 2.1: Microsoft SQL Server<\/a> for the upstream migration notes.<\/li>\n<\/ul>\n<p>We\u2019re excited to bring this support to developers who rely on SQLAlchemy in their everyday work. Whether you\u2019re starting something new or taking care of an application others depend on, we hope it makes the connection to SQL Server and Azure SQL a little easier.<\/p>\n<p>When you\u2019re ready, try the new dialect in your application. We\u2019d welcome hearing what works well and where there\u2019s still room to improve.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We&#8217;re pleased to share that SQLAlchemy 2.1.0 is now generally available, with built-in support for mssql-python, Microsoft&#8217;s Python driver for SQL Server. You can now use it with SQLAlchemy&#8217;s ORM and Core APIs through the first-party mssql+mssqlpython dialect. If SQLAlchemy is already part of how you build, we want this to feel like a natural [&hellip;]<\/p>\n","protected":false},"author":2903,"featured_media":81,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1,690,411,672],"tags":[],"class_list":["post-7642","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure-sql","category-drivers","category-python","category-sql-server-2025"],"acf":[],"blog_post_summary":"<p>We&#8217;re pleased to share that SQLAlchemy 2.1.0 is now generally available, with built-in support for mssql-python, Microsoft&#8217;s Python driver for SQL Server. You can now use it with SQLAlchemy&#8217;s ORM and Core APIs through the first-party mssql+mssqlpython dialect. If SQLAlchemy is already part of how you build, we want this to feel like a natural [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts\/7642","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/users\/2903"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/comments?post=7642"}],"version-history":[{"count":1,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts\/7642\/revisions"}],"predecessor-version":[{"id":7684,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts\/7642\/revisions\/7684"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/media\/81"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/media?parent=7642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/categories?post=7642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/tags?post=7642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}