- changed status to resolved
Incorrect example code in association object documentation
This ticket is against the SQLAlchemy documentation version 0.5.3, updated 02/01/2009 14:16:58.
The "Simplifying Association Object Relations" paragraph has an error in the example code. This line ends up adding the {{{UserKeyword}}} object twice rather than once:
# Adding a Keyword requires creating a UserKeyword association object
user.user_keywords.append(UserKeyword(user, kw1))
Thus, the {{{"print user.keywords"}}} line later on prints:
[Keyword('new_from_blammo'), Keyword('its_big'), Keyword('its_heavy'), Keyword('its_wood')](Keyword('new_from_blammo'),)
This happens because {{{UserKeyword.init}}} is called with the {{{user}}} object, thus already creating the association before the {{{append}}} call is processed. Then {{{append}}} adds the same {{{UserKeyword}}} object a second time. Changing the {{{"print user.user_keywords0"}}} line to {{{"print user.user_keywords"}}} instead will demonstrate the problem, showing that the same {{{UserKeyword}}} object (with the exact same ID) has been added twice.
Solution: Change the example to remove the {{{user.user_keywords.append}}} call, perhaps mentioning that simply creating the {{{UserKeyword}}} object is enough to create the association.
No patch included for the documentation, as I'd rather leave the wording to someone who understands the Assocation Object pattern a little better. If I wrote it, it would probably be a little confusing. :-)
Comments (1)
-
repo owner - Log in to comment
adde312c2a774495d68819765cd07372e384efb6