Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 623471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:05:46+00:00 2026-05-13T19:05:46+00:00

I am having some troubles with the AttributeExtension of SQLAlchemy. Actually I am storing

  • 0

I am having some troubles with the AttributeExtension of SQLAlchemy.

Actually I am storing a de-normalized sum attribute in the Partent table, because I need it quite often for sorting purposes. However, I would like the attribute to get updated whenever the value of one of it’s children is changed.

Unfortunately, the set() method of the AttributeExtension is never called and so, changes aren’t recognized. Using a property-setter which updates also the parent might work, but I would like to know how to use the AttributeExtension of SQLAlchemy (version: 0.6beta2) correctly.

Here is a small (runnable) code snippet which demonstrates the problem:

from sqlalchemy import create_engine, Column, Integer, ForeignKey
from sqlalchemy.orm import relation, scoped_session, sessionmaker, \
         AttributeExtension
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('sqlite:///:memory:', echo=True)
session = scoped_session(sessionmaker(bind=engine, autoflush=True))
Base = declarative_base()
Base.query = session.query_property()

class ChildrenAttributeExtension(AttributeExtension):
    active_history = True

    def append(self, state, child, initiator):
        parent = state.obj()
        parent.sum_of_children += child.value
        return child

    def remove(self, state, child, initiator):
        parent = state.obj()
        parent.sum_of_children -= child.value

    def set(self, state, child, oldchild, initiator):
        print 'set called' # gets never printed
        parent = state.obj()
        parent.sum_of_children += -oldchild.value + child.value
        return child


class Child(Base):
    __tablename__ = 'child'
    id = Column(Integer, primary_key=True)
    parent_id = Column(Integer, ForeignKey('parent.id'), nullable=False)
    value = Column(Integer, nullable=False, default=0)


class Parent(Base):
    __tablename__ = 'parent'
    id = Column(Integer, primary_key=True)
    sum_of_children = Column(Integer, nullable=False, default=0)

    children = relation('Child', backref='parent',
            extension=ChildrenAttributeExtension())

Base.metadata.create_all(engine)

# Add a parent
p = Parent()
session.add(p)
session.commit()

p = Parent.query.first()
assert p.sum_of_children == 0


# Add a child
c = Child(parent=p, value=5)
session.add(c)
session.commit()

p = Parent.query.first()
assert p.sum_of_children == 5

# Change a child
c = Child.query.first()
c.value = 3
session.commit()  # extension.set() doesn't get called

p = Parent.query.first()
assert p.sum_of_children == 3 # Assertion fails

Thanks for your help!
Christoph

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:05:47+00:00Added an answer on May 13, 2026 at 7:05 pm

    As far as I can see, you are looking for events on child, but change child.value. Something like this should do the trick:

    class ValueAttributeExtension(AttributeExtension):
      ...
    
    class Child(Base):
      ...
      value = ColumnProperty(Column(Integer, nullable=False, default=0), 
                             extension=ValueAttributeExtension()) 
    

    EDIT-1: full working example below:

    from sqlalchemy import create_engine, Column, Integer, ForeignKey
    from sqlalchemy.orm import relation, scoped_session, sessionmaker, AttributeExtension, ColumnProperty
    from sqlalchemy.ext.declarative import declarative_base
    
    engine = create_engine('sqlite:///:memory:', echo=False)
    session = scoped_session(sessionmaker(bind=engine, autoflush=True))
    Base = declarative_base()
    Base.query = session.query_property()
    
    class ValueAttributeExtension(AttributeExtension):
        active_history = True
    
        def append(self, state, child, initiator):
            assert False, "should not be called"
    
        def remove(self, state, child, initiator):
            assert False, "should not be called"
    
        def set(self, state, value, oldvalue, initiator):
            print 'set called', state.obj(), value, oldvalue
            child = state.obj()
            if not(child.parent is None):
                child.parent.sum_of_children += -oldvalue + value
            return value
    
    class ChildrenAttributeExtension(AttributeExtension):
        active_history = True
    
        def append(self, state, child, initiator):
            print 'append called', state.obj(), child
            parent = state.obj()
            parent.sum_of_children += child.value
            return child
    
        def remove(self, state, child, initiator):
            print 'remove called', state.obj(), child
            parent = state.obj()
            parent.sum_of_children -= child.value
    
        def set(self, state, child, oldchild, initiator):
            print 'set called', state, child, oldchild
            parent = state.obj()
            parent.parent.sum_of_children += -oldchild.value + child.value
            #parent.sum_of_children += -oldchild.value + child.value
            return child
    
    class Child(Base):
        __tablename__ = 'child'
        id = Column(Integer, primary_key=True)
        parent_id = Column(Integer, ForeignKey('parent.id'), nullable=False)
        value = ColumnProperty(Column(Integer, nullable=False, default=0),
                        extension=ValueAttributeExtension())
    
    class Parent(Base):
        __tablename__ = 'parent'
        id = Column(Integer, primary_key=True)
        sum_of_children = Column(Integer, nullable=False, default=0)
    
        children = relation('Child', backref='parent',
                            extension=ChildrenAttributeExtension())
    
    Base.metadata.create_all(engine)
    
    # Add a parent
    p = Parent()
    session.add(p)
    session.commit()
    
    p = Parent.query.first()
    assert p.sum_of_children == 0
    
    
    # Add a child
    c = Child(parent=p, value=5)
    session.add(c)
    session.commit()
    
    p = Parent.query.first()
    assert p.sum_of_children == 5
    
    # Change a child
    #c = Child.query.first()
    c.value = 3 # fixed bug: = instead of ==
    session.commit()  # extension.set() doesn't get called
    
    p = Parent.query.first()
    assert p.sum_of_children == 3 # Assertion is OK
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some troubles with displaying a graph. It's a very strange issue, because
i'm having some troubles with regular expressions in ruby. I need to categorize some
I'm having some troubles using Session Variables as they are being used as Reference
I'm developing a website in WordPress and I'm having some troubles with the the
I'm building my first ASP.NET MVC application and I am having some troubles with
I know it's possible with jQuery, but I'm having some serious troubles with this
For some reason I am having troubles with a DBI handle. Basically what happened
I'm having troubles with creating a simple list (some expandable lists are already working).
I am having troubles with jQuery's load function and am hoping for some help.
I'm trying to setup some caching on my site and am having troubles with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.