代码之家  ›  专栏  ›  技术社区  ›  Soutzikevich

PONY ORM实体单向映射出错

  •  1
  • Soutzikevich  · 技术社区  · 7 年前

    实体是否总是需要双向关系?

    我有两张桌子。 表 和; 塔布莱

    表

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

    id =主键

    some_attribute = VARCHAR

    TableB_id =外键

    塔布莱

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

    身份证件 =主键

    某些属性 = VARCHAR

    如何使用pony orm重新创建这种关系?

    我的当前代码:

    # 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.erdiagramerrror:表a.tableb_id的反向属性 找不到

    1 回复  |  直到 7 年前
        1
  •  1
  •   Soutzikevich    7 年前

    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)