[sqlalchemy] Re: disable polymorphic load

2008-08-26 Thread ml

 thats a bug which was fixed post 0.4.7p1.  Its in trunk and is for
 0.4.8.

Great!

And back to the mapper properties. As I found it always do a
polymorphic_fetch=select-like query when dealing with mapper properties
referencing a polymorphic base. This is not very efficient. I would
prefer an union fetch. I realized that it is similar to query().get().
Example follows:

session.query(Base).with_polymorphic(*).filter(Base.id==3).first().x

results in 1 joined select:
SELECT base.id AS base_id, base.id_parent AS base_id_parent, base.kind
AS base_kind, derived.id AS derived_id, derived.x AS derived_x,
derived.id_other AS derived_id_other
FROM base LEFT OUTER JOIN derived ON derived.id = base.id
WHERE base.id = ? ORDER BY base.oid
 LIMIT 1 OFFSET 0

but session.query(Base).with_polymorphic(*).get(3).x

results in 2 selects:
SELECT base.id AS base_id, base.id_parent AS base_id_parent, base.kind
AS base_kind
FROM base
WHERE base.id = ?
SELECT derived.x AS derived_x, derived.id_other AS derived_id_other
FROM base JOIN derived ON derived.id = base.id
WHERE base.id = ?

Both queries gives the same object but the get() generates 2 selects
(like the mapper property fetch). Why?

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



[sqlalchemy] Re: disable polymorphic load

2008-08-26 Thread Michael Bayer


On Aug 26, 2008, at 8:24 AM, ml wrote:


 thats a bug which was fixed post 0.4.7p1.  Its in trunk and is for
 0.4.8.

 Great!

 And back to the mapper properties. As I found it always do a
 polymorphic_fetch=select-like query when dealing with mapper  
 properties
 referencing a polymorphic base. This is not very efficient. I would
 prefer an union fetch. I realized that it is similar to query().get().
 Example follows:

 session.query(Base).with_polymorphic(*).filter(Base.id==3).first().x

 results in 1 joined select:
 SELECT base.id AS base_id, base.id_parent AS base_id_parent, base.kind
 AS base_kind, derived.id AS derived_id, derived.x AS derived_x,
 derived.id_other AS derived_id_other
 FROM base LEFT OUTER JOIN derived ON derived.id = base.id
 WHERE base.id = ? ORDER BY base.oid
 LIMIT 1 OFFSET 0

 but session.query(Base).with_polymorphic(*).get(3).x

 results in 2 selects:
 SELECT base.id AS base_id, base.id_parent AS base_id_parent, base.kind
 AS base_kind
 FROM base
 WHERE base.id = ?
 SELECT derived.x AS derived_x, derived.id_other AS derived_id_other
 FROM base JOIN derived ON derived.id = base.id
 WHERE base.id = ?

 Both queries gives the same object but the get() generates 2 selects
 (like the mapper property fetch). Why?

yup..this is one of many things inheritance related that works  
properly in 0.5


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



[sqlalchemy] Re: disable polymorphic load

2008-08-25 Thread ml

One more thing :-)

Now I have polymorphic_fetch=deferred and in some needed cases I call
query.with_polymorphic(*). All works perfectly. But can I setup
something like with_polymorphic(*) on a mapper property? Because now
all relations to Base are polymorphic-deferred and it does me some troubles.


Michael Bayer napsal(a):
 oh, that.  OK, in the 0.4 series you'd want to set  
 polymorphic_fetch='deferred' on your mapper.  Just leave it that  
 way, as the non-deferred behavior has been removed from 0.5 anyway  
 (its deferred in all cases).   The second table will be fetched as  
 needed.
 
 if you're depending heavily on inheritance I strongly recommend  
 looking at 0.5, this is one area where a significant amount of work  
 has been done.
 
 On Aug 25, 2008, at 11:24 AM, ml wrote:
 
 This is not working:

 Setup:
 
 base = Table(base, metadata,
Column(id, Integer, primary_key=True),
Column(kind, Integer),
 )

 derived = Table(derived, metadata,
Column(id, Integer, ForeignKey(base.id), primary_key=True)
 )

 class Base(object): pass
 class Derived(Base): pass

 mapper(Base, base,
polymorphic_on=base.c.kind,
polymorphic_identity=0,
  )

 mapper(Derived, derived,
inherits=Base,
polymorphic_identity=1,
  )
 

 and this call:
 session.query(Base).with_polymorphic(Base).first()

 will still result in 2 SELECTs and the returned object is Derived
 (tables are filled only with Derived objects)

 SELECT base.id AS base_id, base.kind AS base_kind
 FROM base ORDER BY base.id
 LIMIT 1 OFFSET 0

 SELECT derived.id AS derived_id
 FROM derived
 WHERE %(param_1)s = derived.id

 __main__.Derived object at 0xce1b10

 SQLAlchemy version 0.4.7p1

 I need only 1 SELECT and Base object returned.



 Michael Bayer napsal(a):
 call with_polymorphic passing in only the base class:

 query.with_polymorphic(BaseClass).filter()


 On Aug 25, 2008, at 10:35 AM, ml wrote:

 Hi!

 How can I disable a polymorphic load for a single query? I found a
 hint
 in http://markmail.org/message/2kwbm377j3pdvvqb but I can't find  
 more.

 Thanks for any advice.

 David


 
 
  
 

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



[sqlalchemy] Re: disable polymorphic load

2008-08-25 Thread Michael Bayer


On Aug 25, 2008, at 1:05 PM, ml wrote:


 One more thing :-)

 Now I have polymorphic_fetch=deferred and in some needed cases I  
 call
 query.with_polymorphic(*). All works perfectly. But can I setup
 something like with_polymorphic(*) on a mapper property? Because now
 all relations to Base are polymorphic-deferred and it does me some  
 troubles.

not really.  What kind of problems ?


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



[sqlalchemy] Re: disable polymorphic load

2008-08-25 Thread ml
I created a stripped version of my application's model and I found the
problem. The problem is not in the polymorphism but int the inheritance
condition. I have cycles in my model so I had to use the
inherit_condition. I found that it is a big difference how I choose the
expression sides

inherit_condition=derived.c.id==base.c.id

or

inherit_condition=base.c.id==derived.c.id

But why? Is my inherit_condition incorrect at all?

The example code is attached.



Michael Bayer napsal(a):
 
 On Aug 25, 2008, at 1:05 PM, ml wrote:
 
 One more thing :-)

 Now I have polymorphic_fetch=deferred and in some needed cases I  
 call
 query.with_polymorphic(*). All works perfectly. But can I setup
 something like with_polymorphic(*) on a mapper property? Because now
 all relations to Base are polymorphic-deferred and it does me some  
 troubles.
 
 not really.  What kind of problems ?
 
 
  
 

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---

#!/usr/bin/python
# -*- coding: utf-8 -*-

from sqlalchemy import *
from sqlalchemy.orm import *

#engine = create_engine(postgres://localhost/test, echo=True)
engine = create_engine(sqlite://, echo=True)

metadata = MetaData()

base = Table(base, metadata, 
Column(id, Integer, primary_key=True),
Column(id_parent, Integer, ForeignKey(base.id)),
Column(kind, Integer),
)

derived = Table(derived, metadata, 
Column(id, Integer, ForeignKey(base.id), primary_key=True),
Column(x, Integer),
Column(id_other, Integer),
ForeignKeyConstraint([id_other], [base.id], 
  other_fg, use_alter=True),
)

class Base(object):
def __init__(self, id, parent):
self.id = id
self.parent = parent

class Derived(Base):
def __init__(self, id, parent):
Base.__init__(self, id, parent)
self.x = id

mapper(Base, base,
polymorphic_on=base.c.kind,
polymorphic_identity=0,
polymorphic_fetch=deferred,
properties = {
  children: relation(Base, cascade=all,
backref=backref(parent, remote_side=[base.c.id])),
}
  )

mapper(Derived, derived,
inherits=Base,
#inherit_condition=base.c.id==derived.c.id,
inherit_condition=derived.c.id==base.c.id,
polymorphic_identity=1,
properties = {
  other: relation(Base, cascade=all, post_update=True,
  primaryjoin=derived.c.id_other==base.c.id)
}
  )

metadata.drop_all(engine)
metadata.create_all(engine)

Session = sessionmaker(bind=engine, autoflush=False, transactional=False)
session = Session()

#
# test data - chain 1-2-3

session.save(Derived(3, Derived(2, Derived(1, None
session.save(Derived(6, Derived(5, Derived(4, None
session.flush()
session.clear()

#

q = session.query(Base).with_polymorphic(*).get(3)
assert q.parent.parent.x == 1
q = session.query(Base).with_polymorphic(*).get(6)
assert q.parent.parent.x == 4, q.parent.parent.x



[sqlalchemy] Re: disable polymorphic load

2008-08-25 Thread Michael Bayer


On Aug 25, 2008, at 5:38 PM, ml wrote:

 I created a stripped version of my application's model and I found the
 problem. The problem is not in the polymorphism but int the  
 inheritance
 condition. I have cycles in my model so I had to use the
 inherit_condition. I found that it is a big difference how I choose  
 the
 expression sides

 inherit_condition=derived.c.id==base.c.id

 or

 inherit_condition=base.c.id==derived.c.id

 But why? Is my inherit_condition incorrect at all?

 The example code is attached.

thats a bug which was fixed post 0.4.7p1.  Its in trunk and is for  
0.4.8.



--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



[sqlalchemy] Re: disable polymorphic load

2008-08-25 Thread Michael Bayer

sorry not trunk, the head of the 0.4 branch.



--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---