繁体   English   中英

与create_tables = False的pony.orm中的关系

[英]Relationships in pony.orm with 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“,”结构“。” system_id“,”结构...

提示:也许您想引用“ structures.system_id”列。

这里缺少什么?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM