Andi Albrecht wrote:
>
> class Foo(object):
>   pass
>
> database.mapper(Foo, database.metadata.tables["footable"])
>
> Now I have a rather complex SQL that I would like to execute directly
> instead of using object queries:
>
> sql = "select * from footable where something_really_complex_happens"
> for rp in database.session.execute(sql):
>   f = Foo()
>   for key, value in rp.iteritems():
>     setattr(f, key, value)
>
> The problem is that when I try to save the created object, it tries to
> do an INSERT instead of an UPDATE and that AppenderQueries defined in
> the mapping don't work as they try to build a JOIN condition with None
> instead of the primary key.
>
> There must be something wrong with this approach :) Any tipps how to
> solve this?

the most typical way to use raw SQL is to make sure it has the right
column names and use query.from_statement():

rows = query.from_statement("select the_correct column_names from
<anything>").all()

a variant of the above approach is to pass the live cursor into
query.instances(), but this doesn't really change much:

result = engine.execute("select the_correct column_names from <anything>")
rows = list(query.instances(result))


--~--~---------~--~----~------------~-------~--~----~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to