Pony ORM实体单向映射报错
Pony ORM Entity Unidirectional Mapping gives an error
我有2张桌子。 表A & 表B
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 是独立的并且是被引用的 table,它不应该有任何类型的从自身到 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 works is completely wrong in terms of database normalization方式,也无法以其他方式发挥作用。不应该先花一些时间正确阅读文档,就不要盲目使用模块。
道德课
NE假设。 R阅读您的 D文档。
我有2张桌子。 表A & 表B
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 是独立的并且是被引用的 table,它不应该有任何类型的从自身到 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 works is completely wrong in terms of database normalization方式,也无法以其他方式发挥作用。不应该先花一些时间正确阅读文档,就不要盲目使用模块。
道德课
NE假设。 R阅读您的 D文档。