So I figured out that I can do this:
try:
self.session.delete(row)
self.session.flush()
except sqlalchemy.exc.IntegrityError:
print "Can't do that!"
But that really isn't a lot of help, because I'm now stuck with a useless
transaction. If the user makes a bunch of changes, then tries to delete
something that's still being referenced elsewhere, I have to rollback()
*everything* they've done so far. Not good.
So I either need a way to rollback *just* the dodgy deletion, or a way to
anticipate the IntegrityError and not do the deletion at all. The latter would
be better, but I'm OK with either..
Cheers,
Cam
Cameron Jackson
Engineering Intern
Air Operations
Thales Australia
Thales Australia Centre, WTC Northbank Wharf, Concourse Level,
Siddeley Street, Melbourne, VIC 3005, Australia
Tel: +61 3 8630 4591
[email protected]<mailto:[email protected]> |
www.thalesgroup.com.au<http://www.thalesgroup.com.au>
From: [email protected] [mailto:[email protected]] On
Behalf Of Jackson, Cameron
Sent: Monday, 23 January 2012 11:51 AM
To: [email protected]
Subject: [sqlalchemy] Anticipating an IntegrityError before it happens (or
noticing it immediately after)
I have a certain ORM model that is being referenced from several other models.
I don't want the user to be able to delete rows from that table that are still
being referenced elsewhere.
Ideally I would like something like:
refs = get_references(row)
if refs:
print "Can't delete row because of:"
for ref in refs:
print ref
else:
self.session.delete(row)
Or even just:
if is_being_referenced(row):
print "You can't delete that!"
else:
self.session.delete(row)
If there's no way to do either of those, then I would settle for:
try:
self.session.delete(row)
except sql.exc.DeletionError:
print "You can't delete that!"
#Again, a way to get a list of the offending references would be nice!
I tried doing the above, catching IntegrityError, but the problem is that SQLA
doesn't 'notice' the error and tell me about it until another action occurs
that causes other queries to be run. Is there some way I can make SQLA check
the integrity of the database immediately after the deletion? Preferably
without running unnecessary queries if that's going to cause a performance hit.
Cheers,
Cam
Cameron Jackson
Engineering Intern
Air Operations
Thales Australia
Thales Australia Centre, WTC Northbank Wharf, Concourse Level,
Siddeley Street, Melbourne, VIC 3005, Australia
Tel: +61 3 8630 4591
[email protected]<mailto:[email protected]> |
www.thalesgroup.com.au<http://www.thalesgroup.com.au>
-------------------------------------------------------------------------
DISCLAIMER: This e-mail transmission and any documents, files and previous
e-mail messages attached to it are private and confidential. They may contain
proprietary or copyright material or information that is subject to legal
professional privilege. They are for the use of the intended recipient only.
Any unauthorised viewing, use, disclosure, copying, alteration, storage or
distribution of, or reliance on, this message is strictly prohibited. No part
may be reproduced, adapted or transmitted without the written permission of the
owner. If you have received this transmission in error, or are not an
authorised recipient, please immediately notify the sender by return email,
delete this message and all copies from your e-mail system, and destroy any
printed copies. Receipt by anyone other than the intended recipient should not
be deemed a waiver of any privilege or protection. Thales Australia does not
warrant or represent that this e-mail or any documents, files and previous
e-mail messages attached are error or virus free.
-------------------------------------------------------------------------
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.
-------------------------------------------------------------------------
DISCLAIMER: This e-mail transmission and any documents, files and
previous e-mail messages attached to it are private and confidential.
They may contain proprietary or copyright material or information that
is subject to legal professional privilege. They are for the use of
the intended recipient only. Any unauthorised viewing, use, disclosure,
copying, alteration, storage or distribution of, or reliance on, this
message is strictly prohibited. No part may be reproduced, adapted or
transmitted without the written permission of the owner. If you have
received this transmission in error, or are not an authorised recipient,
please immediately notify the sender by return email, delete this
message and all copies from your e-mail system, and destroy any printed
copies. Receipt by anyone other than the intended recipient should not
be deemed a waiver of any privilege or protection. Thales Australia
does not warrant or represent that this e-mail or any documents, files
and previous e-mail messages attached are error or virus free.
-------------------------------------------------------------------------
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.