On 12 Feb 2010, at 17:43, Michael Bayer wrote:

Ed Singleton wrote:
To partially clarify and answer my own question here (I was very tired
by the time I pasted this last night)

In the case of:

sa
.insert
(mytable
).values(myothertable.select().filter_by(foo=sa.bindparam("bar"))

This doesn't currently work because the bindparam required for the
select statement gets discarded.  Having gone through the way SA
compiles statements, it appears that parameters are used to fill in an
insert statements values.  The only way I can see of getting around
this is that at the time of adding the values, they bindparams are
extracted from the select and added to a new attribute on the insert
statement, and around the time of creating the ExecutionContext, they
are processed and added back into the query.

My previous question about getting both the column names and the
bindparams, I managed to solve by (duh!) getting the column names,
compiling the select statement and then getting the bindparams.  I
would still be interested to know where the bindparams are stored in
an uncompiled select though.  I couldn't work it out.

Sorry if all this is a little fuzzy.  It's my first time at really
trying to understand what SA does behind the scenes.

if you're using the @compiler extension to generate this, the same
compiler object would generate the string representation of both the
insert() and the select(), gathering bindparam() objects from the
structure as it goes. the params structure embedded on the Compiled at
the end is what gets sent to execute() along with any ad-hoc values.

The example here:
http://www.sqlalchemy.org/docs/reference/ext/compiler.html?highlight=compiler#compiling-sub-elements-of-a-custom-expression-construct
should accomplish all this.


You mean I've spent a day googling and two days reading SA source code and that's all I had to do? I am both miffed and delighted at the same time.

I didn't know about this compiler extension and had been hacking away at the SA source to get it working as a default feature of insert statements. I'll try this out post-haste.

Thanks again

Ed


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