[storm] Beginner question: adding referenced objects to store

James Henstridge james at jamesh.id.au
Wed Jul 1 05:33:47 BST 2009


On Wed, Jul 1, 2009 at 2:43 AM, Neil de Carteret<storm-list at n3dst4.com> wrote:
> I've come unstuck while trying to learn the basics of Storm by writing
> a basic wiki:
>
> I have a simple many-to-many relationship, between page and tags. I
> have Page class, a Tag class, and a PageTag class, and three matching
> tables. When I set the tags used by a page, I'd like to able to say:
>
>>>> page.tags = [Tag(u"mytag"), Tag(u"othertag"), Tag(u"thirdtag")]

This syntax isn't supported.  The fact that you can execute it with no
error sounds like a bug (probably need to add a ReferenceSet.__set__()
method).  Could you file a bug about this?

> where "page" is an instance of my Page storm class, and Tag is the
> storm class for tags (with an appropriate __init__.)
>
> The problem I'm having is that the tag objects are not getting
> persisted when I "store.commit()".
>
> If I explicitly say:
>
>>>> mytag = Tag(u"mytag")
>>>> store.add(mytag)
>>>> ...etc
>
> Then the tags are saved. Which is a start, but still nothing goes into
> the linker table. And because I don't have any actual PageTag
> instances to play with, I can't store.add() them by hand.
>
> Am I missing the point here? What would be the right way to do this in Storm?

Try doing the following:

  mytag = Tag(u"mytag")
  page.tags.add(mytag)

That will add the tag to the reference set, which will create the
appropriate PageTag linkage rows.  It will also implicitly add the tag
to the store if the page object has been added (or vice versa).  This
is covered in the tutorial here:

https://storm.canonical.com/Tutorial#Many-to-many%20reference%20sets%20and%20composed%20keys


James.



More information about the storm mailing list