summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/build/changelog/changelog_09.rst10
-rw-r--r--lib/sqlalchemy/dialects/mssql/base.py22
2 files changed, 18 insertions, 14 deletions
diff --git a/doc/build/changelog/changelog_09.rst b/doc/build/changelog/changelog_09.rst
index 313e34193..a4d5b9b68 100644
--- a/doc/build/changelog/changelog_09.rst
+++ b/doc/build/changelog/changelog_09.rst
@@ -15,6 +15,16 @@
:version: 0.9.5
.. change::
+ :tags: bug, mssql
+ :tickets: 3025
+
+ Revised the query used to determine the current default schema name
+ to use the ``database_principal_id()`` function in conjunction with
+ the ``sys.database_principals`` view so that we can determine
+ the default schema independently of the type of login in progress
+ (e.g., SQL Server, Windows, etc).
+
+ .. change::
:tags: bug, sql
:tickets: 3024
diff --git a/lib/sqlalchemy/dialects/mssql/base.py b/lib/sqlalchemy/dialects/mssql/base.py
index 522cb5ce3..9a8cddd98 100644
--- a/lib/sqlalchemy/dialects/mssql/base.py
+++ b/lib/sqlalchemy/dialects/mssql/base.py
@@ -1216,22 +1216,16 @@ class MSDialect(default.DefaultDialect):
self.implicit_returning = True
def _get_default_schema_name(self, connection):
- user_name = connection.scalar("SELECT user_name()")
- if user_name is not None:
- # now, get the default schema
- query = sql.text("""
+ query = sql.text("""
SELECT default_schema_name FROM
sys.database_principals
- WHERE name = :name
- AND type = 'S'
- """)
- try:
- default_schema_name = connection.scalar(query, name=user_name)
- if default_schema_name is not None:
- return util.text_type(default_schema_name)
- except:
- pass
- return self.schema_name
+ WHERE principal_id=database_principal_id()
+ """)
+ default_schema_name = connection.scalar(query)
+ if default_schema_name is not None:
+ return util.text_type(default_schema_name)
+ else:
+ return self.schema_name
@_db_plus_owner
def has_table(self, connection, tablename, dbname, owner, schema):