如何在 Elixir 和 Pylons 的多态继承中创建自我关系?
我是编程新手,正在按照 Pylons 文档中有关创建 Wiki 的示例进行操作。我想要链接到 wiki 的数据库是用 Elixir 创建的,因此我重写了 Wiki 数据库架构并从那里继续。
在 wiki 中有一个 的要求由页面和部分继承的导航表。一个部分可以有多个页面,而一个页面只能有一个部分。此外,每个兄弟节点可以相互链式引用。
所以:
- Nav 有“section”(OneToMany)和“before”(OneToOne - 引用前一个节点)
- Page 有“section”(ManyToOne - 一个部分中的多个页面)并继承“before”
- 部分继承了 Nav 的所有内容
代码 I'写的看起来像这样:
class Nav(Entity):
using_options(inheritance='multi')
name = Field(Unicode(30), default=u'Untitled Node')
path = Field(Unicode(255), default=u'')
section = OneToMany('Page', inverse='section')
after = OneToOne('Nav', inverse='before')
before = OneToMany('Nav', inverse='after')
class Page(Nav):
using_options(inheritance='multi')
content = Field(UnicodeText, nullable=False)
posted = Field(DateTime, default=now())
title = Field(Unicode(255), default=u'Untitled Page')
heading = Field(Unicode(255))
tags = ManyToMany('Tag')
comments = OneToMany('Comment')
section = ManyToOne('Nav', inverse='section')
class Section(Nav):
using_options(inheritance='multi')
收到的错误:
sqlalchemy.exc.OperationalError:(OperationalError)表导航没有名为aftr_id的列u'INSERT INTO nav(名称,路径,aftr_id,row_type)VALUES(?,?,?,? )'
我也尝试过:
before = ManyToMany('Nav', inverse='before')
在导航上希望这可以解决问题,但也没有。
本教程中这些声明的原始 SQLAlchemy 代码如下:
nav_table = schema.Table('nav', meta.metadata,
schema.Column('id', types.Integer(),
schema.Sequence('nav_id_seq', optional=True), primary_key=True),
schema.Column('name', types.Unicode(255), default=u'Untitled Node'),
schema.Column('path', types.Unicode(255), default=u''),
schema.Column('section', types.Integer(), schema.ForeignKey('nav.id')),
schema.Column('before', types.Integer(), default=None),
schema.Column('type', types.String(30), nullable=False)
)
page_table = schema.Table('page', meta.metadata,
schema.Column('id', types.Integer, schema.ForeignKey('nav.id'), primary_key=True),
schema.Column('content', types.Text(), nullable=False),
schema.Column('posted', types.DateTime(), default=now),
schema.Column('title', types.Unicode(255), default=u'Untitled Page'),
schema.Column('heading', types.Unicode(255)),
)
section_table = sa.Table('section', meta.metadata,
schema.Column('id', types.Integer,
schema.ForeignKey('nav.id'), primary_key=True),
)
orm.mapper(Nav, nav_table, polymorphic_on=nav_table.c.type, polymorphic_identity='nav')
orm.mapper(Section, section_table, inherits=Nav, polymorphic_identity='section')
orm.mapper(Page, page_table, inherits=Nav, polymorphic_identity='page', properties={
'comments':orm.relation(Comment, backref='page', cascade='all'),
'tags':orm.relation(Tag, secondary=pagetag_table)
})
非常感谢任何帮助。
I am new to programming and am following the example in the Pylons documentation on creating a Wiki. The database I want to link to the wiki was created with Elixir so I rewrote the Wiki database schema and have continued from there.
In the wiki there is a requirement for a Navigation table which is inherited by Pages and Sections. A section can have many pages, while a page can only have one section. In addition, each sibling node can be chain-referenced to each other.
So:
- Nav has "section" (OneToMany) and "before" (OneToOne - to reference preceeding node)
- Page has "section" (ManyToOne - many pages in one section) and inherits "before"
- Section inherits all from Nav
The code I've written looks like this:
class Nav(Entity):
using_options(inheritance='multi')
name = Field(Unicode(30), default=u'Untitled Node')
path = Field(Unicode(255), default=u'')
section = OneToMany('Page', inverse='section')
after = OneToOne('Nav', inverse='before')
before = OneToMany('Nav', inverse='after')
class Page(Nav):
using_options(inheritance='multi')
content = Field(UnicodeText, nullable=False)
posted = Field(DateTime, default=now())
title = Field(Unicode(255), default=u'Untitled Page')
heading = Field(Unicode(255))
tags = ManyToMany('Tag')
comments = OneToMany('Comment')
section = ManyToOne('Nav', inverse='section')
class Section(Nav):
using_options(inheritance='multi')
Errors received on this:
sqlalchemy.exc.OperationalError: (OperationalError) table nav has no column named aftr_id u'INSERT INTO nav (name, path, aftr_id, row_type) VALUES (?, ?, ?, ?)'
I've also tried:
before = ManyToMany('Nav', inverse='before')
on Nav in the hopes this might break the problem, but also not.
The original SQLAlchemy code from the tutorial for these declarations is as follows:
nav_table = schema.Table('nav', meta.metadata,
schema.Column('id', types.Integer(),
schema.Sequence('nav_id_seq', optional=True), primary_key=True),
schema.Column('name', types.Unicode(255), default=u'Untitled Node'),
schema.Column('path', types.Unicode(255), default=u''),
schema.Column('section', types.Integer(), schema.ForeignKey('nav.id')),
schema.Column('before', types.Integer(), default=None),
schema.Column('type', types.String(30), nullable=False)
)
page_table = schema.Table('page', meta.metadata,
schema.Column('id', types.Integer, schema.ForeignKey('nav.id'), primary_key=True),
schema.Column('content', types.Text(), nullable=False),
schema.Column('posted', types.DateTime(), default=now),
schema.Column('title', types.Unicode(255), default=u'Untitled Page'),
schema.Column('heading', types.Unicode(255)),
)
section_table = sa.Table('section', meta.metadata,
schema.Column('id', types.Integer,
schema.ForeignKey('nav.id'), primary_key=True),
)
orm.mapper(Nav, nav_table, polymorphic_on=nav_table.c.type, polymorphic_identity='nav')
orm.mapper(Section, section_table, inherits=Nav, polymorphic_identity='section')
orm.mapper(Page, page_table, inherits=Nav, polymorphic_identity='page', properties={
'comments':orm.relation(Comment, backref='page', cascade='all'),
'tags':orm.relation(Tag, secondary=pagetag_table)
})
Any help is much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你的模型大部分是正确的。我发现的唯一的东西是 Nav->Page 和返回的链接
section
:本教程只是
Section
(不是Page
)是parent
(Nav
类),因此您应该:基本上,为了使模型清晰,请将
Section
视为Directory
,其中Page
就像一个File
。它们都有一个(父)部分
,并且假设它们以某种方式排序,也有before
引用。before
和after
部分对我来说看起来是正确的。因此,唯一剩下的就是数据库模式不反映对象模型。您可以重新创建数据库模型吗?或者至少也发布生成的数据库脚本?I think that your model is mostly correct. The only thing I found is the link
section
from Nav->Page and back:The tutorial just that the
Section
(notPage
) is theparent
(classNav
), so you should instead have:Basically, to make a model clear, see
Section
as aDirectory
, wherePage
is like aFile
. Both of them havea (parent) section
and, assuming that they are sorted in some way, also have thebefore
reference.The part with
before
andafter
looks correct to me. So the only thing remaining is that you database schema does not reflect the object model. Can you re-create the db model? Or at least post resulting db scripts as well?