Hi All,

Say I am creating an application whereby 'organizations' can register
and have multiple 'pages'. So, I have 'org' table and 'page' table.

I wish to maintain the total space used by an orgainzation in
org.size_bytes. So, whenever a page is added, updated or deleted, I
have to increment or decrement org.size_bytes accordingly.

Thinking MapperExtension would be the best fit for this requirement (is
not so?), I write this test code given below, which passes perfectly.
But strangely, while I see the database table (using pgAdmin client)
after executing the code, org.size_bytes is not updated!

Help needed!

thanks
sanjay

from sqlalchemy import *
from sqlalchemy.ext.assignmapper import assign_mapper
from sqlalchemy.ext.sessioncontext import SessionContext

context = SessionContext(create_session)
session = context.current

metadata = BoundMetaData('postgres://user:[EMAIL PROTECTED]/mydb',
echo=False)

# table definitions
org_table = Table('org', metadata,
    Column('id', Integer, primary_key=True),
    Column('name', Unicode(30), nullable=False),
    Column('size_bytes', Integer, nullable=False, default=0))

page_table = Table('page', metadata,
    Column('id', Integer, primary_key=True),
    Column('org_id', Integer, ForeignKey('org.id')),
    Column('content', Unicode))

metadata.drop_all()
metadata.create_all()

class Org(object):
    pass

class Page(object):
    pass

class PageExtension(MapperExtension):

    def before_insert(self, mapper, connection, instance):
        instance.org.size_bytes += len(instance.content)

assign_mapper(context, Org, org_table, properties = {
   'pages' : relation(Page, backref='org')})
assign_mapper(context, Page, page_table, extension=PageExtension(),
    properties = {'content' : deferred(page_table.c.content)})

w = Org(name="My Site")
session.flush()
w.pages.append(Page(content="My Home Page"))
session.flush()
assert w.size_bytes == len("My Home Page")  # passes
del w
w = Org.get_by(name="My Site")
assert w.size_bytes == len("My Home Page")  # passes!
# now go and see the database table org!
# strange that org.size_bytes is not updated!!


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