Reputation: 19837
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?
Upvotes: 1
Views: 198
Reputation: 19837
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!
Upvotes: 1
Reputation: 19377
I'm not savvy in Elixir but this is how you can do it using SQLAlchemy declarative. Hopefully the Elixir definition will be similar enough that this help you.
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class Employee(Base):
__tablename__ = 'employee'
id = Column(Integer, primary_key=True)
name = Column(String(64), nullable=False)
Employee.manager_id = Column(Integer, ForeignKey(Employee.id))
Employee.manager = relationship(Employee, backref='subordinates',
remote_side=Employee.id)
Note that the manager
and manager
_id fields are "monkey-patched" because you cannot make self-references within a class definition.
Upvotes: 2