Hello Mike.

Thanks for this fix, works fine now.

Jürgen

Michael Bayer wrote:
I just committed this change, and re-tested the examples/polymorph/polymorph.py example against mysql for it:

Index: lib/sqlalchemy/orm/util.py
===================================================================
--- lib/sqlalchemy/orm/util.py  (revision 1468)
+++ lib/sqlalchemy/orm/util.py  (revision 1469)
@@ -24,7 +24,15 @@
def polymorphic_union(table_map, typecolname, aliasname='p_union'):
     colnames = sets.Set()
     colnamemaps = {}
-    for table in table_map.values():
+
+    for key in table_map.keys():
+        table = table_map[key]
+
+ # mysql doesnt like selecting from a select; make it an alias of the select
+        if isinstance(table, sql.Select):
+            table = table.alias()
+            table_map[key] = table
+
         m = {}
         for c in table.c:
             colnames.add(c.name)


this does something roughly equivalent to changing the polymorph.py example to say:

# create a union that represents both types of joins.
person_join = polymorphic_union(
    {
        'engineer':people.join(engineers),
        'manager':people.join(managers),
'person':people.select(people.c.type=='person').alias('plain_people'),
    }, None, 'pjoin')
I thought there must be something simple like this.
SA can definitely do to much :)



On May 17, 2006, at 2:30 AM, j.kartnaller wrote:

This is for the actual Schema branch (Sa 0.2 rev. 1468).

Using polymorphism as it is shown in the examples
creates a SQL query like this :

SELECT pjoin.info AS pjoin_info,
       pjoin.parent_id AS pjoin_parent_id,
       pjoin.type AS pjoin_type,
       pjoin.id AS pjoin_id
FROM (
  SELECT info.info AS info,
         contained.parent_id AS parent_id,
         contained.type AS type,
         info.id AS id
  FROM contained JOIN info ON contained.id = info.id
     UNION ALL
       SELECT NULL AS info,
              parent_id,
              type,
              id
       FROM (
         SELECT contained.id AS id,
                contained.parent_id AS parent_id,
                contained.type AS type
         FROM contained
         WHERE contained.type = "contained"
       )
) AS pjoin
ORDER BY pjoin.id

Everything works fine with SQLite.

With MySQL this produces :
SQLError: (OperationalError) (1248, 'Every derived table must have its own alias')


Using an alias in the select of the UNION ALL solves the problem :

SELECT pjoin.info AS pjoin_info,
       pjoin.parent_id AS pjoin_parent_id,
       pjoin.type AS pjoin_type,
       pjoin.id AS pjoin_id
FROM (
  SELECT info.info AS info,
         contained.parent_id AS parent_id,
         contained.type AS type,
         info.id AS id
  FROM contained JOIN info ON contained.id = info.id
     UNION ALL
       SELECT NULL AS info,
              pjoin_1.parent_id,
              pjoin_1.type,
              pjoin_1.id
       FROM (
         SELECT contained.id AS id,
                contained.parent_id AS parent_id,
                contained.type AS type
         FROM contained
         WHERE contained.type = "contained"
       ) as pjoin_1                      <----------------------------
) AS pjoin
ORDER BY pjoin.id


Jürgen



-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users



-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=k&kid0709&bid&3057&dat1642



-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to