Yannick Gingras wrote:
> 
> Hi, I use Alchemy to connect to a legacy system with horrible table
> and column names.  Here is a snippet to give you an idea:
> 
>   po_details_table = Table('F4311', meta.metadata, 
>                          Column("PDKCOO", String, primary_key=True),
>                          Column("PDDOCO", Integer, primary_key=True),
>                          Column("PDDCTO", String, 
>                                 ForeignKey(order_rules_table.c.FSDCTO), 
>                                 primary_key=True),
>                          Column("PDSFXO", String, primary_key=True),
>                          Column("PDLNID", Integer, primary_key=True),
>                          
>                          # Dates
>                          Column("PDDRQJ", JDEDate), 
>                          Column("PDTRDJ", JDEDate), 
> 
>                          # Desc
>                          Column("PDDSC1", Unicode), 
>                          Column("PDDSC2", Unicode),
> 
>                          # Status
>                          Column("PDNXTR", String, 
>                                 ForeignKey(order_rules_table.c.FSTRTY)), 
>                          Column("PDLTTR", String, 
>                                 ForeignKey(order_rules_table.c.FSTRTY)), 
>                          Column("PDLNTY", String, 
>                                 ForeignKey(order_rules_table.c.FSLNTY)),
> 
>                          schema="PRODDTA")
> 
> As you can imagine, I quickly added a few synonyms to my objects:
> 
>   def add_synonyms(props, synonyms):
>       """ expand a {k:v} mapping to a list of synonym properties """
>       props.update(dict([(k, synonym(v)) for k, v in synonyms.items()]))
>       return props
> 
>   mapper(PODetail, po_details_table, 
>            properties=add_synonyms(
>         dict(status_rule_next=relation(OrderRule, primaryjoin=_next_join), 
>              status_rule_last=relation(OrderRule, primaryjoin=_last_join)), 
>         dict(company="PDCO", 
>              company_order_key="PDCOO", 
>              business_unit="PDMCU", 
>              
>              order_type="PDDCTO",
>              line_type="PDLNTY",
> 
>              document_id="PDDOCO", 
>              line_no="PDLNID", 
> 
>              date_requested="PDDRQJ", 
>              date_transaction="PDTRDJ")
>         ) 
>      )  
> 
> So far so good but I still need to do the joins with the column names
> in po_details_table and those don't have human friendly synonyms.  Is
> there a way to create my synonyms mapping in the table object instead
> in doing it in the object mapper?

Yes, columns can be renamed with the key= argument:

   Column("PDDRQJ", JDEDate, key='date_requested')

The key name replaces the true database column name everywhere in Python 
code, so you won't need the mapper options any longer.  Note that 
everywhere includes specifying foreign key references:

   Column("PDDCTO", String,
          ForeignKey(order_rules_table.c.order_type),
          primary_key=True)

and when specifying FKs as strings:

   Column("PDDCTO", String,
          ForeignKey('F3434whatever.order_type'),
          primary_key=True)

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