cryptic/confusing error when using particular parent/child relation (child pk is also fk to parent)

Issue #1406 resolved
Former user created an issue

The attached code dies with a very confusing error/backtrace, which goes away if you comment-out ''either'' of the lines indicated by the XXX comments in the code. The backtrace is included below.

I'm not sure if what the code is doing is supposed to be valid or not, but if it isn't valid it would be great if there could be an obvious error message thrown, it would save a huge amount of time (it took about 4 days to narrow this down as the cause, the original problem cropped up in a much larger and more complicated codebase, and only sporadically, etc).

This is with SA 0.5.3 on Ubuntu 9.04.

Traceback (most recent call last):
  File "/home/jacob/Workspace/dev/Jacob/mumble/safkpkbugrepro.py", line 39, in <module>
    s.flush()
  File "/home/jacob/Workspace/kits/trunk2/output/site-packages/SQLAlchemy-0.5.3-py2.6.egg/sqlalchemy/orm/session.py", line 1351, in flush
    self._flush(objects)
  File "/home/jacob/Workspace/kits/trunk2/output/site-packages/SQLAlchemy-0.5.3-py2.6.egg/sqlalchemy/orm/session.py", line 1422, in _flush
    flush_context.execute()
  File "/home/jacob/Workspace/kits/trunk2/output/site-packages/SQLAlchemy-0.5.3-py2.6.egg/sqlalchemy/orm/unitofwork.py", line 236, in execute
    if up.preexecute(self):
  File "/home/jacob/Workspace/kits/trunk2/output/site-packages/SQLAlchemy-0.5.3-py2.6.egg/sqlalchemy/orm/unitofwork.py", line 666, in preexecute
    self.processor.preprocess_dependencies(self.targettask, elements, trans, delete=False)
  File "/home/jacob/Workspace/kits/trunk2/output/site-packages/SQLAlchemy-0.5.3-py2.6.egg/sqlalchemy/orm/dependency.py", line 264, in preprocess_dependencies
    uowcommit.register_object(child)
  File "/home/jacob/Workspace/kits/trunk2/output/site-packages/SQLAlchemy-0.5.3-py2.6.egg/sqlalchemy/orm/unitofwork.py", line 123, in register_object
    if not self.session._contains_state(state):
  File "/home/jacob/Workspace/kits/trunk2/output/site-packages/SQLAlchemy-0.5.3-py2.6.egg/sqlalchemy/orm/session.py", line 1314, in _contains_state
    return state in self._new or self.identity_map.contains_state(state)
  File "/home/jacob/Workspace/kits/trunk2/output/site-packages/SQLAlchemy-0.5.3-py2.6.egg/sqlalchemy/orm/identity.py", line 103, in contains_state
    return dict.get(self, state.key) is state
AttributeError: 'NoneType' object has no attribute 'key'

Comments (6)

  1. Former user Account Deleted

    Minor update. There's an additional way to make the problem go away (don't specify the pk of the 'parent' object). Also it doesn't matter if the child's pk is also an fk. I updated the attached .py file to reflect this.

  2. Mike Bayer repo owner

    this bug is fixed in d8c9dcc0ad108d7f85cfa41181e5a959783fdd65. But note that your test case is nonsensical, in that it makes no sense for both columns in the primary join condition to be foreign keys (ideally that condition should be raising an error). Also, you're equating an Integer column to a String for which you are making Edgar Codd very, very sad (and without even a CAST which will make every database other than SQLite similarly sad).

  3. Former user Account Deleted

    Thanks for the fix! BTW some of the silliness in the test case was a typo (oops!), and part of it was an artifact of paring down to the minimal repro case...

  4. Log in to comment