如何创建与其自身具有 ManyToMany 关系的 Elixir 类
我很难思考这个问题,但基本上我想创建一个名为 Assets 的 Elixir 类,它可以拥有许多资产。所以,它可能看起来像这样(但这显然不起作用):
class Asset(Entity):
has_field('Name', Unicode)
has_many('Assets', of_kind='Asset', inverse='Assets')
所以,我希望能够有一个“扁平”的资产系统,但我不确定它是否可能,甚至是最好的。
有办法做到这一点吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对 Elixir 不太了解,但这是使用 SQLAlchemy 声明式来做到这一点的方法。希望 Elixir 的定义足够相似,这对您有帮助。
请注意,
manager
和manager
_id 字段是“猴子修补”的,因为您无法在类定义中进行自引用。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.
Note that the
manager
andmanager
_id fields are "monkey-patched" because you cannot make self-references within a class definition.感谢@wberry 的一些见解,我明白了。在 Elixir 中:
使用它,我可以做这样的疯狂事情:
而且它有效。我喜欢它!
I figured it out, thanks to some insight from @wberry. In Elixir:
Using that, I can do crazy things like this:
And it works. I love it!