James / Jurgen,<div><br></div><div>Thanks for spotting my silly mistake. Your help is greatly appreciated.</div><div><br></div><div>Pete.<br><br><div class="gmail_quote">2009/9/18 James Henstridge <span dir="ltr"><<a href="mailto:james@jamesh.id.au">james@jamesh.id.au</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div><div></div><div class="h5">On Fri, Sep 18, 2009 at 12:07 AM, peter websdell<br>
<<a href="mailto:flyingdeckchair@googlemail.com">flyingdeckchair@googlemail.com</a>> wrote:<br>
> Hello all,<br>
> Can anyone help me see what I'm doing wrong here?<br>
><br>
> SQL<br>
> CREATE TABLE IF NOT EXISTS `author` (<br>
> `id` int(8) NOT NULL auto_increment,<br>
> `first_name` varchar(32) NOT NULL,<br>
> `last_name` varchar(32) NOT NULL,<br>
> PRIMARY KEY (`id`)<br>
> ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;<br>
><br>
> CREATE TABLE IF NOT EXISTS `book` (<br>
> `id` int(8) NOT NULL auto_increment,<br>
> `title` varchar(64) NOT NULL,<br>
> `author_id` int(8) NOT NULL,<br>
> PRIMARY KEY (`id`)<br>
> ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;<br>
><br>
> PYTHON<br>
> database = create_database("mysql://uname:password@localhost:3306/DB")<br>
> store = Store(database)<br>
> class Book(Storm):<br>
> __storm_table__ = "book"<br>
> id=Int(primary=True)<br>
> title = Unicode()<br>
> author_id=Int()<br>
> author = Reference(author_id,"Author.id")<br>
> def __init__(self, title):<br>
> self.title = title<br>
><br>
> class Author(Storm):<br>
> __storm_table__="author"<br>
> id=Int(primary=True)<br>
> first_name=Unicode()<br>
> last_name=Unicode()<br>
> books= ReferenceSet(id,"Book.id")<br>
<br>
</div></div>^^^ This is the bug. You've set up your one to many relationship<br>
between Author's primary key and Book's primary key, rather than with<br>
Book's foreign key pointing back to Author. Try changing it to the<br>
following:<br>
<br>
books = ReferenceSet(id, Book.author_id)<br>
<div class="im"><br>
> def __init__(self, first_name, last_name):<br>
> self.first_name = first_name<br>
> self.last_name = last_name<br>
><br>
> dog=store.add(Book(u"Dog Called Demolition"))<br>
> rankin=store.add(Author(u"Robert", u"Rankin"))<br>
> store.flush()<br>
> print "rankin id is %i" % (<a href="http://rankin.id" target="_blank">rankin.id</a>)<br>
> print "dog id is %i" % (<a href="http://dog.id" target="_blank">dog.id</a>)<br>
> rankin.books.add(dog)<br>
<br>
</div>Due to the above bug, this statement will attempt to set <a href="http://dog.id" target="_blank">dog.id</a> to<br>
<a href="http://rankin.id" target="_blank">rankin.id</a>. dog.author_id remains None, which is why dog.author is<br>
also None.<br>
<div class="im"><br>
> print "rankin book 1 = %s" %(rankin.books.one().title)<br>
> print "dog author name = %s" % (dog.author.first_name)<br>
> The error given is:<br>
> Traceback (most recent call last):<br>
> File "models.py", line 33, in <module><br>
> print "dog author name = %s" % (dog.author.first_name)<br>
> AttributeError: 'NoneType' object has no attribute 'first_name'<br>
><br>
> I thought storm should automatically assign the author_id to the book object<br>
> when the book is added to the Author-object "books" set.<br>
<br>
</div>It should. If it doesn't with the above bug fixed, please tell us.<br>
<font color="#888888"><br>
James.<br>
</font></blockquote></div><br></div>