[storm] Normalization in Storm

Leonard Norrgard vinsci at refactor.fi
Thu Mar 13 18:14:03 GMT 2008

Building on the examples in the Storm tutorial
(https://storm.canonical.com/Tutorial), here are two questions:

Question 1:

Finding a person without advance knowledge

How can we find out if we know anything about a person with a certain
name, assuming we do not know what kind of person it is (Accountant,
Employee or a plain Person object)?

Since all Accountant and Employee objects inherit the Person class,
one might expect the following to work:

    >>> store.find(Person, Person.name.like == u'Frank %')

But we get None because in the database Frank's name is stored in the
accountant table and the store.find() operation doesn't take any
derived classes into consideration when searching.

Of course if we already knew Frank was an Accountant (and in the
unlikely case that we would actually be willing to hard-code the query
with Frank's class), finding him is easy:

    >>> store.find(Accountant, Accountant.name.like(u'Frank %')).one().name
    Frank Fourt

Doing a store.find() for each derived class is simply too ugly to be
considered as a solution.

Apparently a Storm design decision has been made to not normalize the
storage of attributes in derived classes, leading to the name
attribute of the Person class being stored all over the database for
the tutorial example. This is of course evident from the SQL
statements in the tutorial, but just to make it clear for all readers:

mysql> select * from person order by id;
| id | name          |
|  1 | Joe Johnes    |
|  2 | Mary Margaret |

mysql> select * from employee order by id;
| id | name       | company_id |
|  1 | Ben Bill   |          2 |
|  2 | Mike Mayer |          2 |

mysql> select * from accountant order by id;
| id | name        |
|  1 | Karl Kent   |
|  2 | Frank Fourt |

Question 2:

Persons who are also Employees and Accountants

A couple of years down the line, Frank finds that he really enjoys
working with Circus Inc. and accepts their offer to become an employee
of theirs.  Since he wants to keep his other accounting client, Sweets
Inc., he also remains as an accountant with them. Frank is now a
Person who is an Employee of Circus Inc. and an Accountant with Sweets

What is the recommended way to handle this use case with Storm?


-- Leonard

Leonard Norrgard       www.refactor.fi
vinsci at refactor.fi     Consulting and software development services.

More information about the storm mailing list