On Nov 15, 2007, at 1:40 PM, Ronald Lew wrote:

>
> I am having issues creating a read-only column using column_property.
> I have a phone table that has the columns: id, country_code, number,
> and deleted.  I want to have an international column which will be set
> to True or False (or 1 or 0) depending on the value of the
> country_code.  Provided below is the snippet of code to work with:
>
> phone_table = Table('phones', metadata,
>    Column('id', Integer, primary_key=True),
>    Column('country_code', String(5)),
>    Column('number', String(20)),
>    Column('deleted', Boolean, default=False),
> )
>
> class Phone(object):
>    pass
>
> # international gets set to True or None which isn't intended
> # also the session is from turbogears.database import session
> session.mapper(Phone, phone_table, properties={
>
> 'international
> ':column_property
> ((phone_table.c.country_code=='1').label('international'))
> })
>
>
>
> I'm not sure if this is a Turbogears issue or SQLAlchemy issue but the
> international field is always None when I do the following: I
> instantiate a Phone object, populate the attributes, session.save the
> instantiated Phone object, session.flush, and then query the new Phone
> object.

this wont work unless you refreshed or expired the instance, or  
expired the individual instance attribute (which is a feature we will  
have in an upcoming release, although i can show you the non-public  
way to do it right now if you were interested).  SA has no idea  
otherwise that it should reload the "international" attribute after a  
flush().

however, a much better way to do it which doesnt require an additional  
SELECT issued to the database would be a straight property:

class Phone(object):
     def international(self):
         return self.country_code == 1
     international = property(international)





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

Reply via email to