SQLAlchemy generates wrong SELECTs for classes that are mapped to
select expressions. Consider the following example:

from sqlalchemy import *

metadata = MetaData("...", echo=True)

table = Table("test", metadata,
              Column("id", Integer, primary_key=True),
              Column("data", String))

table.create()

table.insert().execute([{"data": 1}, {"data": 2}])

test = table.select(table.c.id != 1).alias("test")

class Test(object):
    pass

mapper(Test, test)

referer = Table("referer", metadata,
                Column("id", Integer, primary_key=True),
                Column("fk", Integer, ForeignKey("test.id")))

referer.create()

referer.insert().execute([{"fk": 2}])

class Referer(object):
    pass

mapper(Referer, referer, properties={"ref": relation(Test)})

session = create_session()

t = session.query(Test).get(2)
print t
r = session.query(Referer).get(1)
print r.fk, r.ref


This prints among others:

SELECT test.data AS test_data, test.id AS test_id
FROM (SELECT test.id AS id, test.data AS data
FROM test
WHERE test.id != %(test_id)s) AS test
WHERE test.id = %(test_id_1)s ORDER BY test.id
2007-10-16 15:37:21,817 INFO sqlalchemy.engine.base.Engine.0x..34
{'test_id_1': None, 'test_id': 2}

SELECT referer.fk AS referer_fk, referer.id AS referer_id
FROM referer
WHERE referer.id = %(referer_id)s ORDER BY referer.id
{'referer_id': 1}
SELECT test.data AS test_data, test.id AS test_id
FROM (SELECT test.id AS id, test.data AS data
FROM test
WHERE test.id != %(test_id)s) AS test
WHERE test.id = %(test_id_1)s ORDER BY test.id
{'test_id_1': None, 'test_id': 2}


The first nested SELECT is wrong: test_id should be 1 and test_id_1
should be 2. The same holds for the third SELECT that tries to follow
the foreign key.

Therefore, the program prints
None
2 None

Best regards
  Klaus


--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to