On Aug 10, 1:56 pm, "Mike Orr" <[EMAIL PROTECTED]> wrote:
> Er, where is it you're not supposed to use .c?  The code in MikeB's
> example seems to have .c in every possible location.  How do you
> access a column without .c?


ive clarified this in 0.4's behavior.

lets take a query like this:

select([users.c.id])

which is:

"select id from users".

if we do like a join against some other table on it, like this:

s = select([users.c.id])

select([addresses.c.user_id, s.c.id], addresses.c.user_id==s.c.id)

which is:

select addresses.user_id, id from addreses,
(select id from users) where addresses.user_id=id

we use the "c" attribute on the selectable "s" to get at the "id"
column.  this is because "s" is just another relation (i.e.
"selectable"), with its own list of columns, just like a regular
table.

But if we want to use "s" in a column expression, like this:

select * from addresses where user_id=(select id from users where
name='ed')

we are comparing a scalar column "user_id" to another scalar value.
the subquery is *required* by SQL to return a single, scalar result.
its invalid to say:

select * from addresses where user_id=(select id, name from users
where name='ed')

and it would also be invalid (according to SQL) if the subquery
returned multiple rows.

so whats really happening is, the subquery acts like a column
expression itself.  this is what a "scalar" subquery is.

so in 0.4, you say:

s = s.as_scalar()

and its no longer a "Select" object, its actually a "_ScalarSelect".
and it no longer has a "c" attribute..because its specifically a
"column expression" now, and not a "relation/selectable" like it was
before.

SQLAlchemy has been pretty casual about this kind of thing, like is it
a relation, is it scalar, etc. but Ive tried to clarify these roles
more explicitly in 0.4.

it will still work in 0.4 if you don't say "as_scalar()", in fact.
but if you've specifically declared a subquery as "scalar", it *is*
its own column, and you no longer get at a "c" attribute.


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to