First of I apologize for (a) Not knowing Python all that well (< 2 months),
(b) Not reading through the SQLAlchemy source, and (c) Not looking through
the archives for the correct way to post example code. That said, here's
what I think should be working that is not.
I have a table "gen_item" which contains a ForeignKey Reference to a code
table "state_code".
If I create an ORM instance with a mapper and change the instances foreign key
column "state_id" the relation to the "state_code" table doesn't get updated
and I can't find a method that might refresh its content.
Here is a complete example as simple as I think I can get a full example.
Just in case it gets eaten by the mail system here's a URL to the same code:
http://www.sr.unh.edu/~rea/SQLAlchemy/testFKrelation.py
from sqlalchemy import *
class State(object): pass
class GenItem(object): pass
engine = create_engine('sqlite', {'filename':'testFKrelation.db'})
GenItem.t = Table('gen_item', engine, autoload=True)
State.t = Table('state_code', engine, autoload=True)
print dir(GenItem)
if GenItem.t is None:
print "Creating Tables"
State.t = Table( # State Codes
'state_code', engine,
Column('state_id', Integer, primary_key=True),
Column('state_name', String(12)),
redefine=True)
State.t.create()
State.t.insert().execute(
{'state_id':0, 'state_name':'Have'},
{'state_id':1, 'state_name':'Need'})
GenItem.t = Table( # Generic Items
'gen_item', engine,
Column('item_id', Integer, Sequence('s_item_id'),
primary_key=True),
Column('state_id', Integer, ForeignKey("state_code.state_id")),
Column('item_name', String(30)),
redefine=True)
GenItem.t.create()
GenItem.t.insert().execute(
{'item_id':5, 'item_name':'Milk', 'state_id':0},
{'item_id':6, 'item_name':'Bread', 'state_id':1})
engine.commit()
State.m = mapper(State, State.t)
GenItem.m = mapper( GenItem, GenItem.t, order_by=GenItem.t.c.item_name,
properties={ 'state':relation(State.m)})
milk = GenItem.m.get_by_item_id(5)
print "I %s %s(state_id=%d)"%(
milk.item_name, milk.state.state_name, milk.state_id)
if milk.state_id:
milk.state_id = 0
print "changed from Want to Have"
else:
milk.state_id = 1
print "changed from Have to Want"
objectstore.commit()
# If the above commit doesn't update the relation
# something like objectstore.freshen(milk.state) would be helpful
print "I %s %s(state_id=%d)"%(
milk.item_name, milk.state.state_name, milk.state_id)
--
--------------------------------------------------------------
Robert E. Anderson email: [EMAIL PROTECTED]
Systems Programmer phone: (603) 862-3489
UNH Research Computing Center fax: (603) 862-1761
--------------------------------------------------------------
-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users