]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Revised the query used to determine the current default schema name
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 17 Apr 2014 19:36:43 +0000 (15:36 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 17 Apr 2014 19:36:43 +0000 (15:36 -0400)
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). fixes #3025

doc/build/changelog/changelog_09.rst
lib/sqlalchemy/dialects/mssql/base.py

index 313e34193958a571d4784340a92e374ab4cfb7a7..a4d5b9b68a1b60bbbda9f2dae95bbaeea5a139e9 100644 (file)
 .. changelog::
     :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
index 522cb5ce364b66db03e3795fb057ba9c64db3ae0..9a8cddd9887b3c567cb022bcb51d5f030fcf4dff 100644 (file)
@@ -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):