Association proxy "init" does not work symmetrically

Issue #3235 duplicate
Boris Kozinsky created an issue

For some reason when I try to define association proxy in the user-keyword example, it only works in one direction but not the other, even if I define the association proxies symmetrically (please see attached code). For instance, I can do user1.keywords.append(keyword1) but not keyword1.users.append(user1). The second one will give a "Key Error".

I traced this behavior to the order of arguments in the UserKeyword.init method. The behavior is correct only one way, depending on the argument order, but never both ways. It is somewhat hard to know the right order of arguments for the collection to work...

Comments (4)

  1. Mike Bayer repo owner

    there's a feature lots of people want which is missing here, however there's no bug, as the parameters passed to UserKeyword are not passed by name, they are passed positionally, essentially the single argument. so without using the "creator" argument here, only "name" is getting passed to UserKeyword. See http://docs.sqlalchemy.org/en/rel_0_9/orm/extensions/associationproxy.html#creation-of-new-values:

    The example works here because we have designed the constructor for Keyword to accept a single positional argument, keyword. For those cases where a single-argument constructor isn’t feasible, the association proxy’s creational behavior can be customized using the creator argument, which references a callable (i.e. Python function) that will produce a new object instance given the singular argument.

  2. Mike Bayer repo owner

    see also #647 #2808. I would like to find some way to improve this eventually, probably using introspection, but it isn't high priority right now.

  3. Log in to comment