> When selecting against joined tables, SQLAlchemy always use the
> "ON join_condition" form.
> The problem with this is that when the joined columns have the same
> name, they are included 2 times in the result set.
Only if you select all of those columns. There's no need to resort to side effects of unusual SQL syntaxes here, if you're already using the expression language to generate joins, then you should be specifying those columns you care about selecting when you call select(). Instead of passing "*" to select(), pass the Table object you care about. The names of the columns should be the least important thing here - the expression language deals in Table and Column objects first and foremost.
> In alternative, this can be requested explicitly, as example:
> class Join(FromClause):
> __visit_name__ = 'join'
> When specified ``using`' take precedence over `onclause`.
you can always build a subclass of Join and use @compiles to get at whatever you want, but I think the approach is a hacky way at getting at some other behavior through loose associations.
> On Nov 7, 2012, at 6:18 PM, Manlio Perillo wrote:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>> Hi.
>> When selecting against joined tables, SQLAlchemy always use the
>> "ON join_condition" form.
>> The problem with this is that when the joined columns have the same
>> name, they are included 2 times in the result set.
> Only if you select all of those columns.
> There's no need to resort to side effects of unusual SQL syntaxes
> here, if you're already using the expression language to generate
> joins, then you should be specifying those columns you care
> about selecting when you call select().
I care about selecting all columns, but without having duplicates.
And this is currently not possible, unless I use `use_labels` or manual
labels.
> Instead of passing "*" to select(), pass the Table object you care
> about. The names of the columns should be the least important
> thing here - the expression language deals in Table and Column
> objects first and foremost.
when executing the output is (SQLAlchemy 0.7.7):
2012-11-08 12:49:29,274 INFO sqlalchemy.engine.base.Engine SELECT
foo.id, foo.x, bar.a, bar.id
FROM foo JOIN bar ON foo.id = bar.id
2012-11-08 12:49:29,280 INFO sqlalchemy.engine.base.Engine ()
(u'ID', 777, 0, u'ID')
[u'id', u'x', u'a', u'id']
Traceback (most recent call last):
File "ambiguous.py", line 41, in <module>
print r[0]['ID']
sqlalchemy.exc.InvalidRequestError: Ambiguous column name 'ID' in result
set! try 'use_labels' option on select statement.
How can I avoid this exception?
use_labels is not an option, for me, and manually settings labels in
order to avoid ambiguos columns is a bit of nuisance (I'm actually doing
this).
Thanks Manlio
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
also, you need to not use UPPERCASE names when targeting result columns unless the real column name is not also uppercase - in 0.8 (which you'll need to get reduce_columns()), "case insensitivity" is now off by default: