[storm] Best way to implement One-to-one relationships?
James Henstridge
james at jamesh.id.au
Fri Aug 28 03:25:20 BST 2009
On Thu, Aug 27, 2009 at 7:53 AM, Mac Ryan<quasipedia at gmail.com> wrote:
> Hello everybody,
>
> I am new to the list, new to Storm and fairly new to python...
> somewhere else I would be called a "n00b"! :)
>
> I would like to receive some input on the best way to implement
> one-to-one relationships between tables. I know that one-to-one could be
> a single table... but I am writing a routine for importing in another
> application DB, so I have no choice...
>
> The structure of my DB is:
> TableA - id(primary), field1, field2
> TableB - id(primary), field3, field4, field5...
> where each id is - in a way - the foreign key from the other table.
>
> Currently I implemented it writing in TableA definition:
> extra_fields = Reference(id, TableB.id)
>
> but this implies that when I create ta = TableA() I have my extra
> fields into a sort of "subtree": ta.field1, ta.field2,
> ta.extra_fields.field3, ta.extra_fields.field4, etc... while I would
> like to have them flat at the same level, with ta.field3 and ta.field4
> instead.
>
> However I could not think of any straightforward method to achieve
> this.
>
> Any pointers/help very much appreciated! :)
Hi Mac,
I saw your question on IRC in my scrollback yesterday, but you quit
before I was awake.
In your example code, it looked like you never created a TableB
instance associated with the TableA instance. That is, you were doing
something like:
a = TableA()
a.field1 = 'foo'
a.extra_fields.field3 = 'bar'
You then got an error on that last line because "a.extra_fields" will
be NULL if there is no matching row in TableB (which will be true for
a newly created TableA instance). You can fix this by adding a line
like the following:
a.extra_fields = TableB()
If you add "a" to your store, the reference will take care of adding
"a.extra_fields" as well, and making sure that the IDs match up.
Hope this helps.
James.
More information about the storm
mailing list