Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm using SQLAlchemy to access PostgreSQL database, and I defined the object like this:

class SessionLog(Base):
    __tablename__ = 'session_log'

    id = Column(Integer, primary_key=True)
    recordFile = Column('record_file', String(128))
    appSrcPorts = Column('app_src_ports', ARRAY(Integer))
    info5 = Column('info5', String(100))

and I select and update the session_log table like this:

session = Session()
sessionLog = session.query(SessionLog).filter_by(id=sessionLogId).first()
sessionLog.appSrcPorts.append(1)
session.merge(sessionLog)
session.commit()

But it is weird the column 'app_src_ports' not update after I called merge() and commit(). And I find a ugly way to make it work, before the append() line, add this:

sessionLog.appSrcPorts = list(sessionLog.appSrcPorts)

Anyone can tell me why?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

the SQLAlchemy ORM relies upon detection of events in order to tell when some data has changed, and thus when data needs to be flushed. in this case, you are altering the Python array value in-place, which does not by default produce any events. In order for this to work you need to use the mutable extension in conjunction with the ARRAY type (as well as a list subclass which sends these events) in order for changes to be sent as events related to the parent SessionLog object.

share|improve this answer
    
Do you think it's a SQLAlchemy's bug? –  leeyiw Feb 12 '14 at 7:57
    
well we should have a "MutableList" implementation ready to go in sqlalchemy.ext.mutable, which we don't right now (only MutableDict). And, the ORM could introduce a system by which types can advertise their "mutable" version, thus decreasing the amount of boilerplate in order to use a Mutable type for standard cases like ARRAY. But these would both be enhancements, we don't call those bugs. –  zzzeek Feb 12 '14 at 18:00
    
I see, thanks again! –  leeyiw Feb 13 '14 at 1:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.