The following used to work in SQLAlchemy 0.3:
from sqlalchemy import *
from sqlalchemy.orm import *
metadata = MetaData("...")
metadata.bind.echo=True
table = Table("test", metadata,
Column("id", Integer, primary_key=True),
Column("active", Boolean))
table.create()
table.insert().execute([{"active": False}, {"active": True}])
test = table.select(table.c.active).alias("test")
class Test(object):
pass
mapper(Test, test)
session = create_session()
print session.query(Test).all()
The SQL statement looks fine:
SELECT test.id AS test_id, test.active AS test_active
FROM (SELECT test.id AS id, test.active AS active
FROM test
WHERE test.active) AS test ORDER BY test.id
{}
But it fails with the following traceback:
Traceback (most recent call last):
File "wrong_select_new.py", line 24, in ?
t = session.query(Test).all()
File "build/bdist.linux-i686/egg/sqlalchemy/orm/query.py", line 571,
in all
File "build/bdist.linux-i686/egg/sqlalchemy/orm/query.py", line 619,
in __iter__
File "build/bdist.linux-i686/egg/sqlalchemy/orm/query.py", line 624,
in _execute_and_instances
File "build/bdist.linux-i686/egg/sqlalchemy/orm/query.py", line 680,
in instances
File "build/bdist.linux-i686/egg/sqlalchemy/orm/mapper.py", line
1373, in _instance
File "build/bdist.linux-i686/egg/sqlalchemy/orm/mapper.py", line
873, in identity_key_from_row
File "build/bdist.linux-i686/egg/sqlalchemy/engine/base.py", line
1580, in __getitem__
File "build/bdist.linux-i686/egg/sqlalchemy/engine/base.py", line
1384, in _get_col
File "build/bdist.linux-i686/egg/sqlalchemy/util.py", line 84, in
__getitem__
File "build/bdist.linux-i686/egg/sqlalchemy/engine/base.py", line
1294, in lookup_key
sqlalchemy.exceptions.NoSuchColumnError: "Could not locate column in
row for column 'test.id'"
The only thing that I could find out about the reason is that in
engine/base.py (1290)
context.column_labels
contains the wrong entries. It should contain something like
{..., '<table>_<column>': '<table>_<column>', ...}.
In the case above, however, it contains
{..., '<table>_<column>': '<column>', '<column>': '<column>', ...}.
(That is, it contains two items for each column, but the values are
'<column>' instead of '<table>_<column>'.)
After that, I got lost. These values are generated by the postgres
driver. But I could not find where it takes its input from the Alias
object to generate something different than for the Table object.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---