Mike Lewis wrote:
>
> Hi,
>
> I'm new to SQLAlchemy, and I was wondering if there's a way to test a
> condition while inserting.  I'm trying to make my application use as
> few separate queries at the same time as possible.  Here's a couple
> cases I have had issues with:
> Inserting something with a unique column if it doesn't exist.  Right
> now, I am doing a select with a count() call.
> If I just insert it if it already exists I get an exception, but the
> exception doesn't propogate until the session is flushed.

you can flush() the individual object using flush([myobject]).  if youre
looking to catch a unique constraint exception at that point, SQLAlchemy
version 0.4 exports all the "DBAPI"  errors such as OperationalError,
ProgrammingError, so you can catch the appropriate exception regarding the
constraint.  since this is an "optimistic" approach, depending on the kind
of data you're working with its often the case that the actual exception
is not raised very often.

> Also, I am
> having a tough time catching the SQLAlchemy exceptions.  What's the
> best way to go about this?

theres an exception hierarchy in sqlalchemy/exceptions.py which starts
with SQLAlchemyError.  You can catch that at the most generic level, or
take a look at the exception classes present.

> I have a join table where you have tags and things, and the join table
> tags the things.  I want to test if it already exists and whatnot, and
> insert it in the same query if it doesn't.

im not familiar with a SQL-level "conditional insert, otherwise update"
statement.   The only way I can see this happening (i.e. in a single
query) is to write a stored procedure or trigger which does this.




--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to