[sqlalchemy] Re: Overriding table columns with Python-property

2008-07-22 Thread Malthe Borch

Michael Bayer wrote:
 well, i can support this in 0.5 trunk.  in rev 4965, If a descriptor  
 is present on a class, or if the name is excluded via the include/ 
 exclude lists, the attribute will not be instrumented via the  
 inherited mapper or via the mapped Table.  So your example works with  
 just the @property alone.

The r4965 changeset has the side-effect that any previously instrumented 
attribute will be excluded, too (since ``InstrumentedAttribute`` 
obviously has the __get__-property).

But actually, while I think it's good that any descriptor will be found 
(not only property-derived ones), this changeset does not solve my 
particular issue (the property I wanted to exclude was always excluded 
by ``_should_exclude``).

I'll try to put together an example that correctly demonstrates the 
issue I'm having.

\malthe

--~--~-~--~~~---~--~~
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: Overriding table columns with Python-property

2008-07-22 Thread Malthe Borch

Michael Bayer wrote:
 well, i can support this in 0.5 trunk.  in rev 4965, If a descriptor  
 is present on a class, or if the name is excluded via the include/ 
 exclude lists, the attribute will not be instrumented via the  
 inherited mapper or via the mapped Table.  So your example works with  
 just the @property alone.

I've managed to demonstrate the issue in an isolated test (see below). 
The only change from the previous is that I've set a default value.

This causes SQLAlchemy to *prefetch* the 'col' column, but this throws 
an exception since the column is not mapped.

from sqlalchemy import *
from sqlalchemy.orm import *

e = create_engine('sqlite://')
m = MetaData(e)

t1= Table(
  't1', m,
  Column('id', Integer, primary_key=True),
  Column('col', String(50), default=u),
  )
t1.create()

t2= Table(
  't2', m,
  Column('id', Integer, ForeignKey(t1.id), primary_key=True),
  Column('data', String(50)),
  )
t2.create()

class T1(object):
  pass

class T2(T1):
  @property
  def col(self):
  return uSome read-only value.

polymorphic = (
  [T2], t1.join(t2))

mapper(T1, t1)
mapper(
  T2, t2,
  exclude_properties=('col',),
  with_polymorphic=polymorphic,
  inherits=T1,
  inherit_condition=(t1.c.id==t2.c.id),
  )

sess = sessionmaker()()
x = T2()

assert type(T2.col) is property

x.data = some data
sess.save(x)
sess.commit()
sess.clear()

assert sess.query(T2).one().data == some data
assert sess.query(T2).one().col == uSome read-only value.

x = sess.query(T2).one()
x.data = some new data
sess.commit()
assert sess.query(T2).one().data == some new data


--~--~-~--~~~---~--~~
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: Overriding table columns with Python-property

2008-07-22 Thread Michael Bayer

this issue is not specific to the recent changes;  this would happen  
even with the old behavior (since exclude_properties was meant to  
mean, I dont want SQLA to know about this column at all typically in  
a reflection scenario).  its fixed in r4966.


On Jul 22, 2008, at 7:27 AM, Malthe Borch wrote:


 Michael Bayer wrote:
 well, i can support this in 0.5 trunk.  in rev 4965, If a descriptor
 is present on a class, or if the name is excluded via the include/
 exclude lists, the attribute will not be instrumented via the
 inherited mapper or via the mapped Table.  So your example works with
 just the @property alone.

 I've managed to demonstrate the issue in an isolated test (see below).
 The only change from the previous is that I've set a default value.

 This causes SQLAlchemy to *prefetch* the 'col' column, but this throws
 an exception since the column is not mapped.

 from sqlalchemy import *
 from sqlalchemy.orm import *

 e = create_engine('sqlite://')
 m = MetaData(e)

 t1= Table(
  't1', m,
  Column('id', Integer, primary_key=True),
  Column('col', String(50), default=u),
  )
 t1.create()

 t2= Table(
  't2', m,
  Column('id', Integer, ForeignKey(t1.id), primary_key=True),
  Column('data', String(50)),
  )
 t2.create()

 class T1(object):
  pass

 class T2(T1):
  @property
  def col(self):
  return uSome read-only value.

 polymorphic = (
  [T2], t1.join(t2))

 mapper(T1, t1)
 mapper(
  T2, t2,
  exclude_properties=('col',),
  with_polymorphic=polymorphic,
  inherits=T1,
  inherit_condition=(t1.c.id==t2.c.id),
  )

 sess = sessionmaker()()
 x = T2()

 assert type(T2.col) is property

 x.data = some data
 sess.save(x)
 sess.commit()
 sess.clear()

 assert sess.query(T2).one().data == some data
 assert sess.query(T2).one().col == uSome read-only value.

 x = sess.query(T2).one()
 x.data = some new data
 sess.commit()
 assert sess.query(T2).one().data == some new data


 


--~--~-~--~~~---~--~~
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: Overriding table columns with Python-property

2008-07-20 Thread Michael Bayer


On Jul 19, 2008, at 7:39 AM, Malthe Borch wrote:


 I tried adapting your example, which admittedly works :-), to a  
 scenario
 that better resembles mine, but now the property is overriden simply,
 even when I use ``exclude_properties``.

 Note that the setup is overly complex, but this should be seen in the
 light of a larger setup (as you've previously guided me towards,
 incidentally).

OK, this wasn't really the indended usage of exclude_properties,  
i.e. to block properties from being propagated from a base class where  
they are present.  Right now properties on the base class  
propagate to subclasses unconditionally - theres no way to override  
in a subclass.If I decide to rework this today in 0.5, since I had  
some other issues with the property generation code recently, I'll let  
you know (it'll either work great with minimal effort, or open up a  
whole series of issues I don't have time to deal with).

So for now you need to move col aside to a different name like  
_col in the base class and use properties or synonyms to define the  
behavior of the name col in all cases.




--~--~-~--~~~---~--~~
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: Overriding table columns with Python-property

2008-07-20 Thread Malthe Borch

Michael Bayer wrote:
 well, i can support this in 0.5 trunk.  in rev 4965, If a descriptor  
 is present on a class, or if the name is excluded via the include/ 
 exclude lists, the attribute will not be instrumented via the  
 inherited mapper or via the mapped Table.  So your example works with  
 just the @property alone.

This is good news indeed. Excellent!

\malthe

--~--~-~--~~~---~--~~
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: Overriding table columns with Python-property

2008-07-20 Thread Michael Bayer


On Jul 19, 2008, at 7:39 AM, Malthe Borch wrote:

 I tried adapting your example, which admittedly works :-), to a  
 scenario
 that better resembles mine, but now the property is overriden simply,
 even when I use ``exclude_properties``.

 Note that the setup is overly complex, but this should be seen in the
 light of a larger setup (as you've previously guided me towards,
 incidentally).


well, i can support this in 0.5 trunk.  in rev 4965, If a descriptor  
is present on a class, or if the name is excluded via the include/ 
exclude lists, the attribute will not be instrumented via the  
inherited mapper or via the mapped Table.  So your example works with  
just the @property alone.

--~--~-~--~~~---~--~~
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: Overriding table columns with Python-property

2008-07-19 Thread Malthe Borch

Michael Bayer wrote:
 works for me:

I tried adapting your example, which admittedly works :-), to a scenario 
that better resembles mine, but now the property is overriden simply, 
even when I use ``exclude_properties``.

Note that the setup is overly complex, but this should be seen in the 
light of a larger setup (as you've previously guided me towards, 
incidentally).

from sqlalchemy import *
from sqlalchemy.orm import *

e = create_engine('sqlite://')
m = MetaData(e)

t1= Table(
 't1', m,
 Column('id', Integer, primary_key=True),
 Column('col', String(50)),
 )
t1.create()

t2= Table(
 't2', m,
 Column('id', Integer, ForeignKey(t1.id), primary_key=True),
 Column('data', String(50)),
 )
t2.create()

class T1(object):
 pass

class T2(T1):
 @property
 def col(self):
 return uSome read-only value.

polymorphic = (
 [T2], t1.join(t2))

mapper(T1, t1)
mapper(
 T2, t2,
 exclude_properties=('col',),
 with_polymorphic=polymorphic,
 inherits=T1,
 inherit_condition=(t1.c.id==t2.c.id),
 )

sess = sessionmaker()()
x = T2()

assert type(T2.col) is property

x.data = some data
sess.save(x)
sess.commit()
sess.clear()

assert sess.query(T2).one().data == some data
assert sess.query(T2).one().col == uSome read-only value.

x = sess.query(T2).one()
x.data = some new data
sess.commit()
assert sess.query(T2).one().data == some new data

\malthe

--~--~-~--~~~---~--~~
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: Overriding table columns with Python-property

2008-07-18 Thread Michael Bayer

works for me:

from sqlalchemy import *
from sqlalchemy.orm import *

e = create_engine('sqlite://')
m = MetaData(e)
t= Table('t1', m,
 Column('id', Integer, primary_key=True),
 Column('col', String(50)),
 Column('data', String(50)),
 )
t.create()

class Mapper(object):
 @property
 def col(self):
 return uSome read-only value.

mapper(Mapper, t, exclude_properties=('col',))

sess = sessionmaker()()
x = Mapper()
x.data = some data
sess.save(x)
sess.commit()
sess.clear()

assert sess.query(Mapper).one().data == some data
assert sess.query(Mapper).one().col == uSome read-only value.

x = sess.query(Mapper).one()
x.data = some new data
sess.commit()
assert sess.query(Mapper).one().data == some new data



On Jul 18, 2008, at 2:24 PM, Malthe Borch wrote:


 I have a table 'test' that defines a column 'col'. I map this table  
 on:

 class Mapper(object):
   @property
   def col(self):
 return uSome read-only value.

 passing exclude_properties=('col',).

 However, when I save and commit an instance of Mapper, I get:

 [snip]

 UnmappedColumnError: No column test.col is configured on mapper  
 Mapper...

 This is on SQLAlchemy 0.4.6.

 \malthe


 


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