如何在 Elixir 和 Pylons 的多态继承中创建自我关系?

发布于 2024-08-25 15:31:45 字数 3013 浏览 6 评论 0原文

我是编程新手,正在按照 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

反话 2024-09-01 15:31:45

我认为你的模型大部分是正确的。我发现的唯一的东西是 Nav->Page 和返回的链接 section

class Nav(Entity):
    section = OneToMany('Page', inverse='section')
class Page(Nav):
    section = ManyToOne('Nav', inverse='section')

本教程只是 Section (不是 Page)是parentNav 类),因此您应该:

class Nav(Entity):
    section = ManyToOne('Section')
# and optionally inverse
class Section(Nav):
    children = ManyToOne('Nav')

基本上,为了使模型清晰,请将 Section 视为 Directory,其中Page 就像一个File。它们都有一个(父)部分,并且假设它们以某种方式排序,也有before引用。

beforeafter 部分对我来说看起来是正确的。因此,唯一剩下的就是数据库模式不反映对象模型。您可以重新创建数据库模型吗?或者至少也发布生成的数据库脚本?

I think that your model is mostly correct. The only thing I found is the link section from Nav->Page and back:

class Nav(Entity):
    section = OneToMany('Page', inverse='section')
class Page(Nav):
    section = ManyToOne('Nav', inverse='section')

The tutorial just that the Section (not Page) is the parent (class Nav), so you should instead have:

class Nav(Entity):
    section = ManyToOne('Section')
# and optionally inverse
class Section(Nav):
    children = ManyToOne('Nav')

Basically, to make a model clear, see Section as a Directory, where Page is like a File. Both of them have a (parent) section and, assuming that they are sorted in some way, also have the before reference.

The part with before and after 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文