python - 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”。
这里缺少什么?
解决方案
使用 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')
推荐阅读
- rust - 如何使用 Diesel 在 sqlite 中存储任意 JSON 对象
- ansible - 将变量传递给 Ansible httpapi 插件
- powershell - 尝试将值保存到 Access DB,但在尝试 ExecuteNonQuery 时出现错误
- sql - 在创建外键约束时忽略外键值
- python - 如何解决django中clean函数的问题
- c# - 如何检查光标位置是否在windows窗体中的图表控件之外?
- laravel - BACKPACK Laravel 使用 Backpack CRUD 以多对多关系管理数据透视表中的额外列
- android - 在 RemoteViews 中调用 updateAppWidget 时出现 TransactionTooLargeException(仅在 android 7.0 和 6.0 上)
- json - Camel交换标头中的Json对象未正确转换为字符串
- java - 如何在 Selenium Java 中使用 CTRL + v 在输入字段中粘贴文本值?