I recently tried out sqlalchemy with mssql via pyodbc (after being
bitten by the adodbapi bug with the truncated parameters), and noticed
the following problem:
On inserting records into tables with triggers, pyodbc fails on the
'select @@identity as lastrowid' statement with an 'invalid cursor
state' error.
I've managed to work around this by doing "set nocount on" when
creating the connection, via a creator like this:
def create_connection():
conn = pyodbc.connect(connectionstring)
cur = conn.cursor()
cur.execute('set nocount on')
cur.close()
return conn
I don't know if there's a better way, but it seems to work for me. I
just mention it because it might be a useful tip, and also because it
seems like something that should be handled by the sqlalchemy mssql
driver?
greetings,
Steven
OK, I can't reproduce this (and there's a follow-on issue which
I'll pick up later). Just to clarify, I have this structure
compiled on the database:
<db>
IF OBJECT_ID ('test_audit') IS NOT NULL
DROP TABLE test_audit
GO
IF OBJECT_ID ('test') IS NOT NULL
DROP TABLE test
GO
CREATE TABLE
test
(
id INT NOT NULL IDENTITY PRIMARY KEY,
code VARCHAR (10) NOT NULL UNIQUE
)
GO
CREATE TABLE
test_audit
(
test_id INT NOT NULL FOREIGN KEY REFERENCES test (id),
inserted_on DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
inserted_by VARCHAR (60) NOT NULL DEFAULT SYSTEM_USER
)
GO
CREATE TRIGGER tr_test_i ON test FOR INSERT AS
INSERT INTO test_audit (test_id) SELECT id FROM inserted
GO
</db>
That's a main table (test) an audit table (test_audit)
into which test-INSERTs are triggered. Now, in sqlalchemy:
<code>
from sqlalchemy import *
metadata = BoundMetaData ("mssql://VODEV1/TimHolding")
test = Table ("test", metadata, autoload=True)
result = test.insert ().execute (code = "ABC")
print result.last_inserted_ids ()
# => [1]
</code>
which is what I expected. If I explicitly set NOCOUNT OFF
for my session (in case it's on by default) using:
metadata.engine.raw_connection ().execute ("SET NOCOUNT OFF")
then it still works.
Is my case the situation you're describing? Or have I
misunderstood somthing?
TJG
There is a known issue with retrieving the id of the
last inserted row under MSSQL where IDENTITY cols are
used and there are triggers involved. It's pretty easy
to demonstrate. If I have this construction:
<db>
CREATE TABLE
test
(
id INT NOT NULL IDENTITY PRIMARY KEY,
code VARCHAR (10) NOT NULL UNIQUE
)
GO
CREATE TABLE
test_audit
(
id INT NOT NULL IDENTITY (100, 1) PRIMARY KEY,
test_id INT NOT NULL FOREIGN KEY REFERENCES test (id),
inserted_on DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
inserted_by VARCHAR (60) NOT NULL DEFAULT SYSTEM_USER
)
GO
CREATE TRIGGER tr_test_i ON test FOR INSERT AS
INSERT INTO test_audit (test_id) SELECT id FROM inserted
GO
</db>
and insert something into test:
<db>
INSERT INTO test (code) VALUES ('ABC')
SELECT @@IDENTITY
SELECT * FROM test
</db>
The last id is 100 (the IDENTITY col from [test_audit])
while, as far as the user's concerned, [test] was the
only table affected. In sqlalchemy terms, this means
that the last_inserted_ids () could return misleading
values:
<python>
from sqlalchemy import *
db = BoundMetaData ("mssql://VODEV1/TimHolding")
test = Table ("test", db, autoload=True)
r = test.insert ().execute (code="DEF")
print r.last_inserted_ids ()
# => [101]
list (test.select ().execute (id=101))
# => []
</python>
What are the alternatives? Well, there are two:
IDENT_CURRENT ('tablename')
gives the last identity value assigned to this table
*in any session* (<alert>race condition</alert>)
or
SCOPE_IDENTITY ()
which seems to be what we're after here; it's like @@IDENTITY
but for the same "scope" (not a widely-used term in SQL
Server circles, as far as I know). The documentation
specifically gives this case as an example.
Looks to me like this would be the best bet for sqlalchemy's
purposes, but I'm sure there's a downside somewhere ;)
Comments?
TJG
On Mar 7, 3:29 pm, Tim Golden <tjgol...@gmail.com> wrote:
> <code>
> from sqlalchemy import *
> metadata = BoundMetaData ("mssql://VODEV1/TimHolding")
> test = Table ("test", metadata, autoload=True)
> result = test.insert ().execute (code = "ABC")
> print result.last_inserted_ids ()
> # => [1]
> </code>
>
> which is what I expected. If I explicitly set NOCOUNT OFF
> for my session (in case it's on by default) using:
>
> metadata.engine.raw_connection ().execute ("SET NOCOUNT OFF")
>
> then it still works.
>
> Is my case the situation you're describing? Or have I
> misunderstood somthing?
My fault: I forgot to tell you that I was using a mapped class, and
it's the sqlalchemy-generated 'select @@identity' that causes the
problem. (you can see that it does that in the log output)
> What are the alternatives? Well, there are two:
>
> IDENT_CURRENT ('tablename')
>
> gives the last identity value assigned to this table
> *in any session* (<alert>race condition</alert>)
>
> or
>
> SCOPE_IDENTITY ()
>
> which seems to be what we're after here; it's like @@IDENTITY
> but for the same "scope" (not a widely-used term in SQL
> Server circles, as far as I know). The documentation
> specifically gives this case as an example.
>
> Looks to me like this would be the best bet for sqlalchemy's
> purposes, but I'm sure there's a downside somewhere ;)
>
> Comments?
> TJG
SCOPE_IDENTITY() seems the right choice. I don't know if there are
hidden problems, but if there are, it looks like @@IDENTITY will have
them too. So it seems safer than @@IDENTITY in any case.
I'm happy to provide a patch. Not sure about the connection
variable. Ah, I see, you mean because of backwards compat.
But isn't the problem that if we just leave the @@IDENTITY
as now, it's a danger waiting to happen, especially if the
returned id happens to be a valid id for the table you
*think* it's for?
Not really sure what to offer here:
1) I can provide a patch, replacing @@IDENTITY by SCOPE_IDENTITY
throughout.
2) I can provide a patch allowing connection-level determination
of whether @@IDENTITY or SCOPE_IDENTITY is to be used. (Which
assumes the client module knows what that's about).
3) I can provide a patch which attempts to work out which
one would be allowed from some DB context (or just trying
it to see!)
The problem with (1) is that, if Rick's right, it won't
work with MSSQL <= 7. The problem with any of (2) or (3)
where @@IDENTITY ends up being used is that we might be
silently and dangerously returning wrong data.
TJG
Steven
I'll try it on monday...
FWIW if someone were to be able to review / commit my patch
on ticket 488 (http://www.sqlalchemy.org/trac/ticket/488)
the integrated security would be there anyway. Haven't
got round to patching the SCOPE_IDENTITY stuff yet.
TJG
I've had a go at implementing scope_identity, with mixed results.
It works fine with pymssql
With adodbapi, scope_identity() always returns None
Same problem with pyodbc, and I uncovered a worse problem lurking (it
exists with @@identity)
If you do an insert inside a transaction (i.e. autocommit doesn't
happen), when it tries to extract the ID, you get the dreaded "Invalid
cursor state" error.
So, for the time being I think we should hold fire on scope_identity. I
will see if I can figure out what's up with adodbapi/pyodbc.
Paul
Update: it indeed seems to work like that. I tried changing the order
of preference in mssql.py so that it first tries pydobc, and that
seems to work: the ConcurrentModificationError no longer occurs. I now
also get a warning about using pyodbc that I didn't get before. (by
the way: I did have to keep the 'set nocount on' in order to prevent
the invalid cursor state problem)
I guess something could be done with changing the following line from
the __init__ method of class MSSQLDialect:
self.module = module or dbmodule or use_default()
to something that calls use_pyodbc/use_pymssql/use_adodbapi based on
module.__name__? (I'm not sure though: use_default seems to be called
already when the mssql is imported and it sets the global dbmodule, so
I'm not confident that this is where it should be done*)
Something like this?
{'pyodbc': use_pyodbc, 'adodbapi': use_adodbapi, 'pyodbc':
use_pyodbc}.get(module.__name__, use_default)()
Steven
* can't test it at home (using linux), and as using python at work is
mostly 'under the radar', I can't spend a lot of time on it there, so
sorry if I can't provide you with a well-tested patch ;-)
Sorry, should be pymssql instead of pyodbc twice, but I guess you got
that...
Looks good to me.
I got slightly confused somewhere through this thread.
When I was putting a test together for the passthrough
patch, I ended up using an Import hook to force a
particular dbapi module to be used programatically
(given that I have all three installed).
Obviously there are variations on that (manually renaming
one etc) but have I missed anything more sophisticated
using SA itself? Didn't look like it to me from the code.
TJG
I'll have a look when I get near an MSSQL-connected machine
(tomorrow earliest). Thanks v. much.
TJG
Steven
On 15 mrt, 15:43, "Rick Morrison" <rickmorri...@gmail.com> wrote:
> Sorry, Stephen, I replied too early; your second email arrived before the
> first. A whole day before the first.
>
> So until we get a real cleanup, you're looking to try modules in this order:
>
> ['pyodbc', 'adodbapi', 'pymssql']
>
> Sounds OK to me -- any objections out there?
>
> Rick
>
> On 3/14/07, Rick Morrison <rickmorri...@gmail.com> wrote:
>
>
>
> > It's the second case, that is, it "sniffs" out what modules are installed.
> > As I said before, this
> > (along with other modules that effectively do the same thing), is up for a clean-up soon, see ticket #480.
>
> > Rick
>