首页 > 解决方案 > pony.orm 中与 create_tables=False 的关系

问题描述

目的是让两个简单的类代表数据库中已经存在的两个相关表。
代码是:

from pony.orm import *
db = Database()

class System(db.Entity):
    _table_ = 'some', 'systems'
    
    system_id = PrimaryKey(int, auto=True)
    structures = Set('Structure')

class Structure(db.Entity):
    _table_ = 'some', 'structures'
    structure_id = PrimaryKey(int, auto=True)
    system_id = Required(int)
    system = Required(System)

db.bind(...)
db.generate_mapping(create_tables=False)

我试图遵循我在文档中看到的方法,但是执行上面的代码给了我:

psycopg2.ProgrammingError:列结构。系统不存在
第 1 行:...ctures"."structure_id", "structures"."system_id", "structure...

提示:也许您的意思是引用列“structures.system_id”。

这里缺少什么?

标签: pythondatabase-firstponyorm

解决方案


使用 Pony,您无需为system_id和创建两个单独的属性system。相反,您需要将system_id属性指定为列system。默认情况下,Pony 假定列名等于属性名。然后Structure该类将如下例所示:

class Structure(db.Entity):
    _table_ = 'some', 'structures'
    structure_id = PrimaryKey(int, auto=True)
    system = Required(System, column='system_id')

推荐阅读