Pony ORM实体单向映射给出错误 [英] Pony ORM Entity Unidirectional Mapping gives an error

查看:326
本文介绍了Pony ORM实体单向映射给出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2张桌子. TableA & TableB

I have 2 tables. TableA & TableB

TableA

+------+----------------+-----------+
|  id  | some_attribute | tableB_id |
+------+----------------+-----------+

id =主键

id=primary key

some_attribute = varchar

some_attribute=varchar

TableB_id =外键

TableB_id=foreign key

TableB

+------+----------------+
|  id  | some_attribute |
+------+----------------+

id =主键

id=primary key

some_attribute = varchar

some_attribute=varchar


# establish a connection with the database (MySQL)
db = Connection.Connection.connect_db_server_default()


class TableB(db.Entity):
    _table_ = "table_b"
    id = PrimaryKey(int, auto=False)
    some_attribute = Required(str)


class TableA(db.Entity):
    _table_ = "table_a"
    id = PrimaryKey(int, auto=False)
    some_attribute = Required(str)
    TableB_id = Set(TableA)

出现以下错误:
pony.orm.core.ERDiagramError:TableA.TableB_id的反向属性 找不到

Gives the following error:
pony.orm.core.ERDiagramError: Reverse attribute for TableA.TableB_id not found


如何使用Pony ORM重新建立以上关系? 实体是否总是需要建立双向关系?

How can the above relationship be recreated, using Pony ORM? Do entities always need to have bi-directional relationship?

有什么办法可以实现正确的数据库规范化,还是我必须使用一些不同的ORM模块?

Is there any way at all to achieve proper database normalization or will I have to use some different ORM module?

推荐答案

这似乎是我自己的错误,并且在Pony-ORM文档中指定了答案.

It looks like that the error here is my own and the answer to this is specified in the Pony-ORM documentation.

我认为,由于 TableB 是独立的并且是被引用的表,因此它不应具有从其自身到 TableA 的任何映射.但是,文档指定:

I assumed that since TableB is independent and is the referenced table, it shouldn't have any kind of mapping from itself to TableA. Yet, the documentation specifies:

某些映射器(例如Django)需要在一侧定义关系 只要.小马要求明确定义双方的关系 (如Python的Zen所述:显式优于隐式),其中 允许用户从每个角度查看所有关系 实体.

Some mappers (e.g. Django) require defining relationships on one side only. Pony requires defining relationships on both sides explicitly (as The Zen of Python reads: Explicit is better than implicit), which allows the user to see all relationships from the perspective of each entity.

以及来自文档同一部分中发布的示例:

And from the examples posted at the same section of the documentation:

# If we want to allow an instance of OrderItem to exist without
# being assigned to an 'Order', we can define the order attribute as Optional:
class Order(db.Entity):
    items = Set(lambda: Order)

class OrderItem(db.Entity):
    order = Optional(Order)

即使 Pony-ORM 的工作方式完全错误 >在数据库规范化方面,它无法以任何其他方式起作用.切勿盲目使用模块,除非先花一些时间正确阅读文档.

Even though the way Pony-ORM works is completely wrong in terms of database normalization, it cannot function any other way. A module should never be blindly used, without spending some time first to properly read the documentation.

NE 是假设. R 完善您的 D 提示.

NEver assume. Read your Documentation.

这篇关于Pony ORM实体单向映射给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆