After migrating to SQLalchemy 0.9.8 (python 2.7.6) I had an hour of bugfixing fun:
Running this little test script below in ipython 1.2.1 with autoreload magic on, I encountered this quite weirdly behaving error:
[code lang=”python”]
> import testcase as t
> t.test()
ImportError: importlater.resolve_all() hasn’t been called (this is sqlalchemy.orm session)[/code]
However, if I directly ran the test in the module, I had no problems (uncommenting the line in testcase.py). Trying the same in python command line, the error vanished.
Simple solution: update your ipython client, something with the autoreload behavior of ipython seemed to fuck up things. I am now on ipython 2.3.1 and things work as expected.
testcase.py
[code lang=”python”]
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
prefix = “test”
class Model(Base):
__tablename__ = prefix+”_model”
id = Column(Integer, primary_key=True)
class Population(Base):
__tablename__ = prefix+”_population”
id = Column(Integer, primary_key=True)
model_id = Column(Integer, ForeignKey(prefix+”_model.id”))
model = relationship(“Model”, foreign_keys=model_id)
def test():
a = Model()
b = Population()
b.model = a
# test() # <-- uncomment this line, then it works
[/code]
Full error message
[code lang="python"]
# ---------------------------------------------------------------------------
# ImportError Traceback (most recent call last)
#
# —-> 1 er.test()
# testcase.py in test()
# 24 a = Model()
# 25 b = Population()
# —> 26 b.model = a
# 27
# 28 # test() # <-- uncomment this line, then it works
# /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/orm/attributes.pyc in __set__(self, instance, value)
# 224 def __set__(self, instance, value):
# 225 self.impl.set(instance_state(instance),
# --> 226 instance_dict(instance), value, None)
# 227
# 228 def __delete__(self, instance):
# /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/orm/attributes.pyc in set(self, state, dict_, value, initiator, passive, check_old, pop)
# 810 ))
# 811
# –> 812 value = self.fire_replace_event(state, dict_, value, old, initiator)
# 813 dict_[self.key] = value
# 814
# /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/orm/attributes.pyc in fire_replace_event(self, state, dict_, value, previous, initiator)
# 830 for fn in self.dispatch.set:
# 831 value = fn(
# –> 832 state, value, previous, initiator or self._replace_token)
# 833
# 834 state._modified_event(dict_, self, previous)
# /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.pyc in set_(state, newvalue, oldvalue, initiator)
# 74 return newvalue
# 75
# —> 76 sess = state.session
# 77 if sess:
# 78
#
# /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/orm/state.py in session(self, sessionlib)
# 148 “””Return the owning :class:`.Session` for this instance,
# 149 or “None“ if none available.”””
# –> 150 return sessionlib._state_session(self)
# 151
# 152 @property
# /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.pyc in __getattr__(self, key)
# 909 % self._full_path)
# 910 try:
# –> 911 attr = getattr(self.module, key)
# 912 except AttributeError:
# 913 raise AttributeError(
# /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.pyc in __get__(self, obj, cls)
# 723 if obj is None:
# 724 return self
# –> 725 obj.__dict__[self.__name__] = result = self.fget(obj)
# 726 return result
# 727
# /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.pyc in module(self)
# 894 “importlater.resolve_all() hasn’t ”
# 895 “been called (this is %s %s)”
# –> 896 % (self._il_path, self._il_addtl))
# 897
# 898 return getattr(self._initial_import, self._il_addtl)
# ImportError: importlater.resolve_all() hasn’t been called (this is sqlalchemy.orm session)
[/code]
Leave a Reply