On Nov 1, 2009, at 8:10 PM, Ghost wrote:

> How does one go about doing that? If you need to use the orm to query
> the database for columns that do not exist in the table itself, do you
> not need to add these to the mapper? Can that be done without
> synonyms?

if "do not exist in the table itself", you mean you'd like an  
attribute that represents some concept, such as a string concatenation  
like "first_name || ' ' || last_name", the column_property() construct  
is used for that.    that basic idea is illustrated at 
http://www.sqlalchemy.org/docs/05/mappers.html#sql-expressions-as-mapped-attributes
 
  .

You can also customize further if you'd like to optimize the kinds of  
expressions the attribute generates when querying - the entrypoint to  
those techniques are at 
http://www.sqlalchemy.org/docs/05/mappers.html#custom-comparators 
.


>
> I have thought of a sensical example to illustrate what I am trying to
> do: You want a field for the age of a person even though you only
> store their date of birth in the database table. You want to be able
> to query the database by filtering using the age column so I assume
> you need to add it to the mapper.
>
> Should it be done in the following way or is another approach
> favoured?

your best bet would be to identiify the SQL expression that best suits  
your needs and use column_property().   Doing the work on the SQL side  
allows the concept to be used as a filtering/ordering criterion on the  
database side.

>
> ===
> class Person(object):
>   def _set_age(self, value):
>      pass
>   def _get_age(self):
>      return date.today() - self.date_of_birth
>   age = property(_get_age, _set_age)
>
> mapper(Person, person_table, properties={
>    'age': synonym("date_of_birth")
> })
> ===
>
> Thank you for your help..
> >


--~--~---------~--~----~------------~-------~--~----~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to