gpt4 book ai didi

python - Pony ORM实体单向映射报错

转载 作者:行者123 更新时间:2023-11-29 05:50:47 29 4
gpt4 key购买 nike

我有 2 个表。 TableA & TableB

TableA

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

id=主键

some_attribute=varchar

TableB_id=外键

TableB

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

id=主键

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)

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


我的问题:

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

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

最佳答案

看起来这里的错误是我自己的,对此的答案在 Pony-ORM 文档中指定。

我假设因为 TableB 是独立的并且是引用表,所以它不应该有任何类型的从自身到 TableA 的映射。然而,documentation指定:

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.

以及在文档同一部分发布的示例:

# 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作品在 database normalization 方面完全错误 ,它无法以任何其他方式运行。如果没有先花一些时间正确阅读文档,切勿盲目使用模块。

道德课

NE永远假设。 阅读您的D文档。

关于python - Pony ORM实体单向映射报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54580180/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com