summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2007-06-17 00:49:08 +0000
committerMike Bayer <mike_mp@zzzcomputing.com>2007-06-17 00:49:08 +0000
commit909758df8edf5e319127216bc2c4ce0fe780de21 (patch)
tree327f091acae7e0c40933d71d1c3e6183fb38f3d4 /lib/sqlalchemy
parent23525a3ea876f6ab16566c2dd0bbaa7ec1037052 (diff)
downloadsqlalchemy-909758df8edf5e319127216bc2c4ce0fe780de21.tar.gz
- result.last_inserted_ids() should return a list that is identically
sized to the primary key constraint of the table. values that were "passively" created and not available via cursor.lastrowid will be None. - sqlite: string PK column inserts dont get overwritten with OID [ticket:603]
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r--lib/sqlalchemy/databases/mssql.py15
-rw-r--r--lib/sqlalchemy/databases/mysql.py5
-rw-r--r--lib/sqlalchemy/databases/sqlite.py4
-rw-r--r--lib/sqlalchemy/engine/default.py12
4 files changed, 19 insertions, 17 deletions
diff --git a/lib/sqlalchemy/databases/mssql.py b/lib/sqlalchemy/databases/mssql.py
index 4336296dd..2b6808eac 100644
--- a/lib/sqlalchemy/databases/mssql.py
+++ b/lib/sqlalchemy/databases/mssql.py
@@ -271,13 +271,14 @@ class MSSQLExecutionContext(default.DefaultExecutionContext):
self.cursor.execute("SET IDENTITY_INSERT %s OFF" % self.compiled.statement.table.fullname)
self.IINSERT = False
elif self.HASIDENT:
- if self.dialect.use_scope_identity:
- self.cursor.execute("SELECT scope_identity() AS lastrowid")
- else:
- self.cursor.execute("SELECT @@identity AS lastrowid")
- row = self.cursor.fetchone()
- self._last_inserted_ids = [int(row[0])]
- # print "LAST ROW ID", self._last_inserted_ids
+ if not len(self._last_inserted_ids) or self._last_inserted_ids[0] is None:
+ if self.dialect.use_scope_identity:
+ self.cursor.execute("SELECT scope_identity() AS lastrowid")
+ else:
+ self.cursor.execute("SELECT @@identity AS lastrowid")
+ row = self.cursor.fetchone()
+ self._last_inserted_ids = [int(row[0])] + self._last_inserted_ids[1:]
+ # print "LAST ROW ID", self._last_inserted_ids
self.HASIDENT = False
super(MSSQLExecutionContext, self).post_exec()
diff --git a/lib/sqlalchemy/databases/mysql.py b/lib/sqlalchemy/databases/mysql.py
index 09825bef0..91d59f1e2 100644
--- a/lib/sqlalchemy/databases/mysql.py
+++ b/lib/sqlalchemy/databases/mysql.py
@@ -945,8 +945,9 @@ def descriptor():
class MySQLExecutionContext(default.DefaultExecutionContext):
def post_exec(self):
if self.compiled.isinsert:
- self._last_inserted_ids = [self.cursor.lastrowid]
-
+ if not len(self._last_inserted_ids) or self._last_inserted_ids[0] is None:
+ self._last_inserted_ids = [self.cursor.lastrowid] + self._last_inserted_ids[1:]
+
class MySQLDialect(ansisql.ANSIDialect):
def __init__(self, **kwargs):
ansisql.ANSIDialect.__init__(self, default_paramstyle='format', **kwargs)
diff --git a/lib/sqlalchemy/databases/sqlite.py b/lib/sqlalchemy/databases/sqlite.py
index aaaf55697..8e776fb26 100644
--- a/lib/sqlalchemy/databases/sqlite.py
+++ b/lib/sqlalchemy/databases/sqlite.py
@@ -148,7 +148,9 @@ def descriptor():
class SQLiteExecutionContext(default.DefaultExecutionContext):
def post_exec(self):
if self.compiled.isinsert:
- self._last_inserted_ids = [self.cursor.lastrowid]
+ if not len(self._last_inserted_ids) or self._last_inserted_ids[0] is None:
+ self._last_inserted_ids = [self.cursor.lastrowid] + self._last_inserted_ids[1:]
+
super(SQLiteExecutionContext, self).post_exec()
class SQLiteDialect(ansisql.ANSIDialect):
diff --git a/lib/sqlalchemy/engine/default.py b/lib/sqlalchemy/engine/default.py
index 976da1a73..f295f5fd2 100644
--- a/lib/sqlalchemy/engine/default.py
+++ b/lib/sqlalchemy/engine/default.py
@@ -283,7 +283,6 @@ class DefaultExecutionContext(base.ExecutionContext):
self._lastrow_has_defaults = False
for param in plist:
last_inserted_ids = []
- need_lastrowid=False
# check the "default" status of each column in the table
for c in self.compiled.statement.table.c:
# check if it will be populated by a SQL clause - we'll need that
@@ -291,7 +290,7 @@ class DefaultExecutionContext(base.ExecutionContext):
if c in self.compiled.inline_params:
self._lastrow_has_defaults = True
if c.primary_key:
- need_lastrowid = True
+ last_inserted_ids.append(None)
# check if its not present at all. see if theres a default
# and fire it off, and add to bind parameters. if
# its a pk, add the value to our last_inserted_ids list,
@@ -306,15 +305,14 @@ class DefaultExecutionContext(base.ExecutionContext):
if c.primary_key:
last_inserted_ids.append(param.get_processed(c.key))
elif c.primary_key:
- need_lastrowid = True
+ last_inserted_ids.append(None)
# its an explicitly passed pk value - add it to
# our last_inserted_ids list.
elif c.primary_key:
last_inserted_ids.append(param.get_processed(c.key))
- if need_lastrowid:
- self._last_inserted_ids = None
- else:
- self._last_inserted_ids = last_inserted_ids
+ # TODO: we arent accounting for executemany() situations
+ # here (hard to do since lastrowid doesnt support it either)
+ self._last_inserted_ids = last_inserted_ids
self._last_inserted_params = param
elif self.compiled.isupdate:
if isinstance(self.compiled_parameters, list):