Hi,

I am using the new features of the recent 0.7.3 release for support
for mutation tracking (section 2.10.3 in the documentation) verbatim -
following the example in the documentation. So I have:

Base = declarative_base()

class JSONEncodedDict(TypeDecorator):
    "Represents an immutable structure as a json-encoded string."
    impl = VARCHAR

    def process_bind_param(self, value, dialect) :
        if value is not None:
            value = json.dumps(value)
        return value

    def process_result_value(self, value, dialect):
        if value is not None:
            value = json.loads(value)
        return value



class MutationDict(Mutable, dict):
    @classmethod

    def coerce(cls, key, value):
        "Convert plain dictionaries to MutationDict."

        if not isinstance(value, MutationDict) :
            if isinstance(value, dict) :
                return MutationDict(value)

            ## this call will raise ValueError
            return Mutable.coerce(key, value)
        else :
            return value

    def __setitem__(self, key, value):
        "Detect dictionary set events and emit change events."
        dict.__setitem__(self, key, value)
        self.changed()

    def __delitem__(self, key):
        "Detect dictionary del events and emit change events."
        dict.__delitem__(self, key)
        self.changed()

MutationDict.associate_with(JSONEncodedDict)
==============
followed by a number of class / tables that use the JSONEncodedDict to
pack dictionaries into the database:

class PatentInfo(Base):
    """
    The main patent info table with references to the secondary tables
    """
    __tablename__ = "pat_info_main"
##    id = Column(Integer, primary_key=True)
    pNum = Column(Integer, primary_key=True)
    pStatus = Column(String)
    pAppDate = Column(Integer)
    pPubDate = Column(Integer)
    pEC = Column(JSONEncodedDict)
    pTreeStats = Column(JSONEncodedDict)

    pBibInfo = relationship("BibInfo", backref="pat_info_main",
uselist=False)
    pClassification = relationship("Classification",
backref="pat_info_main", uselist=False)
    pCited = relationship('CitedInfo', backref="pat_info_main",
uselist=False)

    def __init__(self, pDat):
        """PatentInfo Constructor"""
        if isinstance(pDat, PatentDatItem):
            self.pNum = pDat.pat_num
            self.pStatus = pDat.status

etc.

In the code I am merging newly acquired date into the databas:

                pat = PatentInfo(self.pDat)
                bib_dat = BibInfo(self.pDat.bib_dat)
                class_dat =
Classification(self.pDat.bib_dat.classification)
                cite_dat = CitedInfo(self.pDat.cited)
                bib_dat.pat_info_main = pat
                class_dat.pat_info_main = pat
                cite_dat.pat_info_main = pat

                self.db.session.merge(pat)

                self.db.session.commit()

=====
In a number of cases where an actual merge takes place (as opposed to
addition of new data) I got an error message as the following:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/var/folders/mp/mp8U9z96G784zzlU07ugOU+++TI/-Tmp-/
Python485iEc.py", line 272, in <module>
  File "/var/folders/mp/mp8U9z96G784zzlU07ugOU+++TI/-Tmp-/
Python485iEc.py", line 133, in Acquire
  File "ParseResponse.py", line 107, in ParseXML
    self.db.session.merge(pat)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/
lib/python2.6/site-packages/SQLAlchemy-0.7b3-py2.6.egg/sqlalchemy/orm/
session.py", line 1222, in merge
    load=load, _recursive=_recursive)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/
lib/python2.6/site-packages/SQLAlchemy-0.7b3-py2.6.egg/sqlalchemy/orm/
session.py", line 1314, in _merge
    load, _recursive)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/
lib/python2.6/site-packages/SQLAlchemy-0.7b3-py2.6.egg/sqlalchemy/orm/
properties.py", line 135, in merge
    impl.set(dest_state, dest_dict, value, None)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/
lib/python2.6/site-packages/SQLAlchemy-0.7b3-py2.6.egg/sqlalchemy/orm/
attributes.py", line 498, in set
    value, old, initiator)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/
lib/python2.6/site-packages/SQLAlchemy-0.7b3-py2.6.egg/sqlalchemy/orm/
attributes.py", line 504, in fire_replace_event
    value = fn(state, value, previous, initiator or self)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/
lib/python2.6/site-packages/SQLAlchemy-0.7b3-py2.6.egg/sqlalchemy/ext/
mutable.py", line 372, in set
    oldvalue._parents.pop(state.obj(), None)
NameError: global name 'state' is not defined

I tried to follow the errors to their source and it seems as if it is
buried inside the source code for the sqlalchemy.

I am using the most recent version: 0.7b3

So - is that a real bug that needs fix of the source or something that
I am doing wrong?

Thanks,

Rivka


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