My previous description was a bit simplistic. Turns out the problem
only occurs when a join is involved.

See the following code:

###########################################
from sqlalchemy import *
from sqlalchemy import orm

class One(object): pass
class Many(object): pass

sm = orm.sessionmaker(autoflush=True, transactional=True)
session = orm.scoped_session(sm)
engine = create_engine('mysql://u:[EMAIL PROTECTED]/db')
engine.echo = True
metadata = MetaData(engine)

one_table = Table('one', metadata,
                  Column('id', Integer, primary_key=True),
                  Column('a', Integer),
                  Column('b', Integer)
                  )
orm.mapper(One, one_table, properties = { 'many' :
orm.relation(Many) })

many_table = Table('many', metadata,
                   Column('id', Integer, primary_key=True),
                   Column('one_id', Integer, ForeignKey('one.id')))
orm.mapper(Many, many_table )

metadata.create_all()

myquery = session.query(One).filter(One.many.any(Many.c.id==333))  ##
1
myquery = myquery.filter(One.c.a==55)
#myquery = myquery.add_entity(Many).join('many')                    ##
2

print  myquery
###################################################

1) When I run it as it is, i.e. line ##2 is commented out the output
is:

SELECT one.id AS one_id, one.a AS one_a, one.b AS one_b
FROM one
WHERE (EXISTS (SELECT 1
FROM many
WHERE one.id = many.one_id AND many.id = %s)) AND one.a = %s ORDER BY
one.id

which is OK

2) Uncommenting the join in line ##2 causes the following exception
when trying to print the query:
raise exceptions.InvalidRequestError("Select statement '%s' returned
no FROM clauses due to auto-correlation; specify correlate(<tables>)
to control correlation manually." % self)

3) This is the reason I decided to use correlate(). When I added
correlate(Many) in line ##1 like this:
myquery =
session.query(One).filter(One.many.any(Many.c.id==333).correlate(Many))
## 1

I got the following exception when tryint to print the query:
AttributeError: type object 'Many' has no attribute '_cloned_set'

What is my mistake?




On May 27, 5:42 pm, Michael Bayer <[EMAIL PROTECTED]> wrote:
> On May 27, 2008, at 4:39 AM, Moshe C. wrote:
>
>
>
>
>
> > I have the following mapping for classes One and Many:
>
> >     mapper(One, one_table, properties={'many':relation(Many)})
> >     mapper(Many, many_table)
>
> > and the following query:
>
> > session
> > .query(One).filter(One.many.any(Many.name.like(expr)).correlate(Many))
>
> > I get the exception
> > exceptions.AttributeError: type object 'Many' has no attribute
> > '_cloned_set'
>
> > What am I doing wrong ?
>
> > (correlate(None) works, but does not produce the desired query since
> > One is also added to the FROM clause)
>
> correlate() accepts Table objects for now.
>
> but also, it doesnt make any sense since any() is designed to produce
> a subquery which correlates to the outer "One" table; correlating to
> "Many" is meaningless here since "Many" isn't in the enclosing
> select() statement.
>
> If any() is producing a query with a FROM list other than just
> "Many" (and you are not introducing other tables inside of your
> <expr>), that would be amazing since we have dozens and dozens of unit
> tests for any() in all kinds of situations, please produce a working
> test case and file a ticket.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to