On Wed, 12 Nov 2008, Michael Bayer wrote:
> On Nov 12, 2008, at 3:31 PM, Faheem Mitha wrote:
>> 2) In section "Building Complex Views"
>>
>> print broker.holdings[stock].shares
>> # 10
>>
>> Isn't holdings[stock] the share value? Ie. shouldn't this just be
>>
>> print broker.holdings[stock]
>>
>> ?
>
> im going to go out on a limb here since I did not write these docs
Really? Who did? Good job by the author, anyway.
> and suggest it should say:
>
> broker.by_stock[stock] = Holding(broker, stock, 10) print
> broker.by_stock[stock].shares
>
> to illustrate non-association proxy usage, before the next blurb that
> describes the association proxy usage.
Sounds plausible.
>> 4) A more general question:
>>
>> I'm having difficulty parsing the last three lines in the itemized list
>> in Section "associationproxy".
>>
>> "The relation is inspected ..." through "A creator funcion can be
>> used..."
>>
>> In particular, I'm having difficulty understanding the point of the
>> creator function in association_proxy. I see where creator functions
>> are defined (two places), but don't see them being used.
>
> the association proxy manages an association relation across three
> objects, such as:
>
> A->B->C
>
> when you say something along the lines of:
>
> A.someelement = C
>
> the "B" is created implicitly by the association proxy. The creator function
> is used to customize the process of creating the B instance.
Hmm. Adding something like this would not be a bad idea, except with more
elaboration.
Just to repeat, I didn't really follow the point of the last three bullet
points. No doubt I'm being obtuse, but I think a little explicitness would
not hurt here.
* The relation is inspected to determind the type of the related objects.
What relation are we talking about, and what types? What is the point of
determining the types? I guess in this case there are three objects and
two relations as described in your diagram above, namely
A->B->C
* To construct new instances, the type is called with the value being
assigned, or key and value for dicts.
I have no idea what this means. Are the instances referred to instances of
the association object B as above?
* A creator function can be used to create instances intead.
I suppose we are talking about instances of B again.
I'm looking at the simplest example that uses a creator function, namely
the User/Keyword example starting with
def get_current_uid():
Here the creator function is
def _create_uk_by_keyword(keyword):
"""A creator function."""
return UserKeyword(keyword=keyword)
Now, can you point me a line in this example
where there is the equivalent of
A.someelement = C
? In this case it would be something like
user.keywords = Keyword('foo')
Hmm, I suppose
for kw in (Keyword('its_big'), Keyword('its_heavy'), Keyword('its_wood')):
user.keywords.append(kw)
is also creating association objects, since there is one association
object (here, UserKeyword) created for each new Keyword.
In any case, I think it would be helpful to emphasize this point, since it
is not obvious, at least to me. Also, how does creator know where to get
the arguments from in general? Does it come via the "C" object (here
Keywords)? What happens if it isn't specified? Does it fall back to the
constructor instead, or something else?
Incidentally, in the Broker/Stock example, the creator function, namely
def _create_holding(stock, shares):
"""A creator function, constructs Holdings from Stock and share
quantity."""
return Holding(stock=stock, shares=shares)
isn't used anywhere that I can see, even implicitly, since no stocks are
added to holdings in the example.
Well, back to trying to get my association proxy example to work.
Currently, I've got an infinite loop. :-)
>> 5) An even more general comment:
>>
>> There are two discussions on mapping association objects. One of them
>> is in the mapper docs
>> (http://www.sqlalchemy.org/docs/05/mappers.html#advdatamapping_relation_patterns_association)
>> and uses a one-many and many-one relation. The second is in the docs
>> for association proxy
>> (http://www.sqlalchemy.org/docs/05/plugins.html#plugins_associationproxy_building),
>> two examples in "Simplifying Association Object Relations" and
>> "Building Complex Views", and uses association_proxy.
>>
>> It looks like either of these approaches can be used to initialize and
>> update the db. It might be helpful if these two could be connected
>> somehow. Perhaps a see also in the respective sections? I do see a "To
>> enhance the association object pattern such that direct access to the
>> Association object is optional, SQLAlchemy provides the associationproxy."
>> in
>> http://www.sqlalchemy.org/docs/05/mappers.html#advdatamapping_relation_patterns_association,
>> but it seems like you could just use the association_proxy setup described
>> by itself, and it would suffice. The version in
>> http://www.sqlalchemy.org/docs/05/mappers.html#advdatamapping_relation_patterns_association
>> says explicitly that this is the way to go - doesn't clearly point to
>> alternatives.
>
> If I understand the question to mean, "why illustrate two ways", its because
> the main mapping docs are describing a basic relational pattern and
> contrasting it to what is termed "many to many",which is similar except that
> many-to-many does not assign any additional information to the association
> table. Its a core pattern that most non-trivial schemas will need to use.
>
> The association proxy OTOH is not necessary in any case, but is strictly a
> convenience layer used to provide more succinct access in the Python space
> across an association relation. We don't want people even considering its
> usage until they have a firm grasp of the association object pattern, hence
> its treated as an "extra". The association proxy is also not strictly only
> for use with an association relation - it can also be used to provide direct
> access to scalars on related objects, for example, such as representing a
> list of integers or strings. It's really not at all a "relational pattern"
> and is instead a Python attribute access helper.
>
> ticket #1224 is added to address the docs issue.
Yes, I agree with the above. I'm still in the process of learning about
this, and I stated incorrectly that the association proxy defines
relations, which it doesn't. It now looks to me strictly like syntactic
sugar. I don't have anything to add here.
Regards, Faheem.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---