关系小马.orm使用create_tables=Fals

2024-05-29 11:49:10 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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)

我试图遵循我见过的in the documentation方法,但是执行上面的代码会给我带来:

psycopg2.ProgrammingError: column structures.system does not exist
LINE 1: ...ctures"."structure_id", "structures"."system_id", "structure...

HINT: Perhaps you meant to reference the column "structures.system_id".

这里少了什么?在


Tags: 代码idtrueautodbtablesomestructure
1条回答
网友
1楼 · 发布于 2024-05-29 11:49:10

使用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')

相关问题 更多 >

    热门问题