On Dec 22, 2008, at 11:06 AM, Eoghan Murray wrote:

>
> The following example uses an elixir class:
>
> class MyE(Entity):
>    id = Field(Integer, primary_key=True)
>    f_1 = ManyToOne('OtherE')
>    f_2 = ManyToOne('OtherE')
>    date = Field(Date)
>
> MyE.query.select_from(union(MyE.table.select(),
>                           select([MyE.id, MyE.f_1.label('f_2'),
> MyE.f_2.label('f_1')]))).\
>    order_by([MyE.date])

It would help if you could illustrate with accurate code - the UNION  
above does not have consistent numbers of columns in each select() and  
I think what you're trying to do is reverse f_1 and f_2 in the second  
select() statement which is also not illustrated above.

I am able to reproduce your results with the script at the end of this  
email.

Its a fun little problem for which I haven't thought of a solution as  
of yet.  your union delivers both f_1 and f_2 from its second and  
third columns.  If you ask the union, hey which one of your columns  
corresponds to table.c.f_1 , the answer is ambiguous since its both.    
I could change corresponding_column to raise an error for this  
ambiguity, but that wouldnt fix your problem.  The thing you "expect"  
here is that the columns in the *first* select of the union should  
take precedence over the subsequent ones.  A naive change in that  
regard breaks tests which do in fact place columns in subsequent  
selects which need to be counted so something else will have to be  
figured out.   probably some extra guesswork in  
CompoundSelect._populate_column_collection to detect this kind of thing.

Why do all the really intricate problems seem to come from the elixir  
camp ?  :)


from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('sqlite://', echo=True)

Base = declarative_base()

class Other(Base):
     __tablename__ = 'other'
     id = Column(Integer, primary_key=True)

class MyE(Base):
     __tablename__ = 'mine'
     id = Column(Integer, primary_key=True)
     f_1 = Column(Integer, ForeignKey('other.id'))
     f_2 = Column(Integer, ForeignKey('other.id'))
     foreign_1 = relation(Other, primaryjoin=f_1==Other.id)
     foreign_2 = relation(Other, primaryjoin=f_2==Other.id)
     date = Column(Date)

Base.metadata.create_all(engine)

sess = sessionmaker(bind=engine)()

u = union(
             MyE.__table__.select(),
             select([MyE.id, MyE.f_2, MyE.f_1, MyE.date])
         )

# this is why
print ua.corresponding_column(MyE.__table__.c.f_1)
print ua.corresponding_column(MyE.__table__.c.f_2)

print sess.query(MyE).select_from(u).order_by(MyE.date).all()


--~--~---------~--~----~------------~-------~--~----~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to