Hello the Community,

imagine we have a tree-like dictionary:

class IndustrySector(Entity):
    name = Field(Unicode(128),required=True,unique=False)
    parent = ManyToOne('IndustrySector',required=False)
    using_table_options(UniqueConstraint('name','parent_id'))

Top-level items have parent_id of NULL; an example is a top-level item
'IT' and sub-items 'Software development', 'Outsourcing', while
'Software development' has sub-items of 'Linux kernel development',
'Web application development', etc.

Next, we have a Company entity:
class Company(Entity):
    name                = Field(Unicode(128), required=True,
nullable=False)
    industry_sector  = ManyToOne(IndustrySector, required=True)

that is linked to the IndustrySector dictionary.

What is the *right* way to:

1) get the top-level IndustrySector? For example, if 'My Company' has
it's indusrty_sector set to 'Linux kernel development', we'll get 'IT'
as a top-level; I'd suppose to add a read-only property to the Company
entity that selects industry_sector.parent until is is None; Say we
name it 'top_level_industry_sector';

1a) get a list of all industry sectors, from bottom to top; in the
example above, this will return a list of 'Linux kernel development',
'Software development', 'IT'

2) query all the Company that have a required top-level industry
sector? Well, something like
DBSession.query(Company).filter(top_level_industry_sector=1) will go
or not?

It seems it's quite simple to implement these methods but maybe
there's a well-known solution/pattern already and I should not invent
the wheel? :)

Thanks in advance!

--
    Sergei.



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"SQLElixir" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/sqlelixir?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to