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">&lt;<a href="mailto:james@jamesh.id.au">james@jamesh.id.au</a>&gt;</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>
&lt;<a href="mailto:flyingdeckchair@googlemail.com">flyingdeckchair@googlemail.com</a>&gt; wrote:<br>
&gt; Hello all,<br>
&gt; Can anyone help me see what I&#39;m doing wrong here?<br>
&gt;<br>
&gt; SQL<br>
&gt; CREATE TABLE IF NOT EXISTS `author` (<br>
&gt;   `id` int(8) NOT NULL auto_increment,<br>
&gt;   `first_name` varchar(32) NOT NULL,<br>
&gt;   `last_name` varchar(32) NOT NULL,<br>
&gt;   PRIMARY KEY  (`id`)<br>
&gt; ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;<br>
&gt;<br>
&gt; CREATE TABLE IF NOT EXISTS `book` (<br>
&gt;   `id` int(8) NOT NULL auto_increment,<br>
&gt;   `title` varchar(64) NOT NULL,<br>
&gt;   `author_id` int(8) NOT NULL,<br>
&gt;   PRIMARY KEY  (`id`)<br>
&gt; ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;<br>
&gt;<br>
&gt; PYTHON<br>
&gt; database = create_database(&quot;mysql://uname:password@localhost:3306/DB&quot;)<br>
&gt; store = Store(database)<br>
&gt; class Book(Storm):<br>
&gt;     __storm_table__ = &quot;book&quot;<br>
&gt;     id=Int(primary=True)<br>
&gt;     title = Unicode()<br>
&gt;     author_id=Int()<br>
&gt;     author = Reference(author_id,&quot;Author.id&quot;)<br>
&gt;     def __init__(self, title):<br>
&gt;         self.title = title<br>
&gt;<br>
&gt; class Author(Storm):<br>
&gt;     __storm_table__=&quot;author&quot;<br>
&gt;     id=Int(primary=True)<br>
&gt;     first_name=Unicode()<br>
&gt;     last_name=Unicode()<br>
&gt;     books= ReferenceSet(id,&quot;Book.id&quot;)<br>
<br>
</div></div>^^^ This is the bug.  You&#39;ve set up your one to many relationship<br>
between Author&#39;s primary key and Book&#39;s primary key, rather than with<br>
Book&#39;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>
&gt;     def __init__(self, first_name, last_name):<br>
&gt;         self.first_name = first_name<br>
&gt;         self.last_name = last_name<br>
&gt;<br>
&gt; dog=store.add(Book(u&quot;Dog Called Demolition&quot;))<br>
&gt; rankin=store.add(Author(u&quot;Robert&quot;, u&quot;Rankin&quot;))<br>
&gt; store.flush()<br>
&gt; print &quot;rankin id is %i&quot; % (<a href="http://rankin.id" target="_blank">rankin.id</a>)<br>
&gt; print &quot;dog id is %i&quot; % (<a href="http://dog.id" target="_blank">dog.id</a>)<br>
&gt; 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>
&gt; print &quot;rankin book 1 = %s&quot; %(rankin.books.one().title)<br>
&gt; print &quot;dog author name = %s&quot; % (dog.author.first_name)<br>
&gt; The error given is:<br>
&gt; Traceback (most recent call last):<br>
&gt;   File &quot;models.py&quot;, line 33, in &lt;module&gt;<br>
&gt;     print &quot;dog author name = %s&quot; % (dog.author.first_name)<br>
&gt; AttributeError: &#39;NoneType&#39; object has no attribute &#39;first_name&#39;<br>
&gt;<br>
&gt; I thought storm should automatically assign the author_id to the book object<br>
&gt; when the book is added to the Author-object &quot;books&quot; set.<br>
<br>
</div>It should.  If it doesn&#39;t with the above bug fixed, please tell us.<br>
<font color="#888888"><br>
James.<br>
</font></blockquote></div><br></div>