Search code examples
pythonsqlalchemypython-elixir

How to create an Elixir class that has a ManyToMany relationship with itself


I'm having trouble thinking this through, but basically I want to create an Elixir class called Assets which can have many Assets. So, it could look something like this (but this doesn't work, obviously):

class Asset(Entity):
    has_field('Name', Unicode)
    has_many('Assets', of_kind='Asset', inverse='Assets')

So, I would love to be able to have a 'flat' system of Assets, but I'm not sure its possible or even best.

Is there a way of doing this?


Solution

  • I figured it out, thanks to some insight from @wberry. In Elixir:

    class Asset(Entity):
        has_field('Name', Unicode)
        Assets = ManyToMany('Asset')
    

    Using that, I can do crazy things like this:

    a1 = Asset(Name=u'Asset 1')
    a2 = Asset(Name=u'Asset 2')
    a3 = Asset(Name=u'Asset 3')
    
    a1.Assets=[a1,a2,a3]
    

    And it works. I love it!