]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
mssql now able to reflect start and increment values for identity columns
authorPaul Johnston <paj@pajhome.org.uk>
Wed, 11 Jul 2007 17:58:45 +0000 (17:58 +0000)
committerPaul Johnston <paj@pajhome.org.uk>
Wed, 11 Jul 2007 17:58:45 +0000 (17:58 +0000)
CHANGES
lib/sqlalchemy/databases/mssql.py
test/engine/reflection.py

diff --git a/CHANGES b/CHANGES
index b281a8fb6c813e7e7d6154017735c49bcb55a760..9fa41dfb2fd34ec8c41c4e08cf4a946c27d533db 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -96,6 +96,7 @@
 
 - mssql
     - fix port option handling for pyodbc [ticket:634]
+    - now able to reflect start and increment values for identity columns
 
 - extensions
     - added selectone_by() to assignmapper
index b4ae8053fddc4daddb79a1dec5d9b0c803d7dde2..934a644017fd56d013c8cfd75962df184a37c6dd 100644 (file)
@@ -32,9 +32,6 @@ Known issues / TODO:
 
 * No support for more than one ``IDENTITY`` column per table
 
-* No support for table reflection of ``IDENTITY`` columns with
-  (seed,increment) values other than (1,1)
-
 * No support for ``GUID`` type columns (yet)
 
 * pymssql has problems with binary and unicode data that this module
@@ -524,10 +521,8 @@ class MSSQLDialect(ansisql.ANSIDialect):
             raise exceptions.NoSuchTableError(table.name)
 
         # We also run an sp_columns to check for identity columns:
-        # FIXME: note that this only fetches the existence of an identity column, not it's properties like (seed, increment)
-        #        also, add a check to make sure we specify the schema name of the table
-        # cursor = table.engine.execute("sp_columns " + table.name, {})
         cursor = connection.execute("sp_columns " + table.name)
+        ic = None
         while True:
             row = cursor.fetchone()
             if row is None:
@@ -537,6 +532,20 @@ class MSSQLDialect(ansisql.ANSIDialect):
                 ic = table.c[col_name]
                 # setup a psuedo-sequence to represent the identity attribute - we interpret this at table.create() time as the identity attribute
                 ic.sequence = schema.Sequence(ic.name + '_identity')
+                # MSSQL: only one identity per table allowed
+                cursor.close()
+                break
+        if not ic is None:
+            try:
+                cursor = connection.execute("select ident_seed(?), ident_incr(?)", table.fullname, table.fullname)
+                row = cursor.fetchone()
+                cursor.close()
+                if not row is None:
+                    ic.sequence.start=int(row[0])
+                    ic.sequence.increment=int(row[1])
+            except:
+                # ignoring it, works just like before
+                pass
 
         # Add constraints
         RR = self.uppercase_table(ischema.ref_constraints)    #information_schema.referential_constraints
index 344d5a94f5938254d91c21fc265379ada0fe0449..26cbe3722ae26ba10044b9a599af2d591c255d74 100644 (file)
@@ -435,6 +435,23 @@ class ReflectionTest(PersistTest):
         finally:
             table.drop()
 
+    @testbase.supported('mssql')
+    def testidentity(self):
+        meta = MetaData(testbase.db)
+        table = Table(
+            'identity_test', meta, 
+            Column('col1', Integer, Sequence('fred', 2, 3), primary_key=True)
+        )
+        table.create()
+        
+        meta2 = MetaData(testbase.db)
+        try:
+            table2 = Table('identity_test', meta2, autoload=True)
+            print table2.c['col1'].sequence
+        finally:
+            table.drop()
+
+
 class CreateDropTest(PersistTest):
     def setUpAll(self):
         global metadata, users