On Aug 2, 2010, at 12:19 PM, Robert Sudwarts wrote:

> Hi,
> 
> I'm having trouble understanding the correct syntax to be used with 
> Declarative and a column property.
> The select statement I'm using is:  select([func.substr(my_table.c.my_string, 
> 2, 3)]).label(my_substr'), deferred=True
> And (as per the docs using the expanded syntax, this works as expected. 
> 
> With the Declarative syntax, I've tried: 
> 
> from sqlalchemy.util                 import classproperty
> from sqlalchemy.orm                ..., column_property
> 
> class MySubstr(object):
>       @classproperty
>       def my_substr(cls):
>               return column_property(
>                                                  
> select([func.substr(cls.my_str, 2, 3)]).label('my_substr')
>                                                 )
> 
> class MyTable(Base, MySubstr):
>     .....
> 
> and then expect to be able to call a record in MyTable and return its 
> "my_substr", however, all I'm getting is a representation of the object ...
> eg <sqlalchemy.orm.properties.ColumnProperty object at 0xa4951cc>  and no 
> apparent way of seeing its value.

I see nothing wrong with the example, the only potential glitch is that 
"my_str" needs to have a name assigned up front.  You didn't illustrate that 
part here, so FYI it would be extremely helpful if you could attach fully 
working examples.  In this case the example is:

from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.util                 import classproperty
from sqlalchemy.orm import column_property, sessionmaker

Base = declarative_base()

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

class MySubstr(object):
    @classproperty
    def my_substr(cls):
        return column_property(
                               select([func.substr(cls.my_str, 2, 
3)]).label('my_substr')
                          )

class MyTable(Base, MySubstr):
    __tablename__ = 'foo'
    id = Column(Integer, primary_key=True)
    my_str = Column('my_str', String)


Base.metadata.create_all(engine)

sess = sessionmaker(engine)()
m1 = MyTable(my_str='some string')
sess.add(m1)
sess.commit()

print m1.my_substr

and it works fine.



> 
> Should the @classproperty sit within the "MyTable(Base)" itself (ie is there 
> a need for the separate object --

@classproperty is typically specific to the mixin use case, which is why its 
only discussed in the "mixins" section of the declarative documentation.  It's 
a useful construct in other situations but you certainly don't need it if you 
aren't using mixins.


> and have I been confused by the section on 'mixins' )?  

I'm not sure why you're choosing to use a mixin here, as your column_property 
appears to be pretty specific to the MyTable class.   if the documentation led 
you to believe that you needed one, I am extremely curious why that is the case 
as mixins are absolutely optional and that would be a serious documentation 
bug.     Without the mixin, it is:

class MyTable(Base):
    __tablename__ = 'foo'
    id = Column(Integer, primary_key=True)
    my_str = Column('my_str', String)
    my_substr = column_property(
                           select([func.substr(my_str, 2, 
3)]).label('my_substr')
                      )



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