I am trying to use the association_proxy for attributes that link to
tables containing mostly static data

e.g. my static data is:
COUNTRY
COUNTRY.CODE
COUNTRY.NAME

and the data I am changing is:
USER
USER.COUNTRY_CODE

I use the association_proxy as I want to be able to say:
user = User()
user.country_name = 'DENMARK'
#rather than  user.country_cde = 'DK'

In class User()   (declarative)
I have:
country_code = Column(String, ForeignKey('COUNTRY.CODE'))
country = relation('Country', uselist=False)
country_name = association_proxy('country', 'name',
creator=my_creator) #proxy to COUNTRY.NAME

where:
def my_creator(country_name):
    country = session.query(Country).filter_by(name=country_name).one
()


i.e. I will only link to existing countries.. and not add a new one.

Is there a better way to do this?

In this instance I can create a new session in my_creator, but I would
really want to be using the same session, incase I have added, deleted
countries inside the same transaction.  Making the session global
doesn't seem right.

thanks


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