天道酬勤,学无止境

ponyorm

An attempt to mix objects belonging to different transactions

问题 pony.orm.core.TransactionError: An attempt to mix objects belonging to different transactions 我知道这个错误是描述性的,我只是无法弄清楚它为什么会发生。 我搜索了谷歌、文档、其他 SO 帖子,但一无所获。 有任何想法吗? 回答1 如果有人试图互连属于两个不同数据库会话的身份映射的两个对象,则会出现此错误: from pony.orm.examples.university1 import * populate_database() with db_session: g = Group[101] with db_session: s = Student[4] s.group = g # or g.students.add(s) 每个 db 会话应仅与在此 db 会话中检索到的对象一起使用。 如果您需要在 db 会话之间存储一些信息,您可以存储对象的id值而不是对象本身。 您也可以在一个db_session中腌制对象,然后在另一个db_session中取消腌制它们。

2022-07-11 09:57:22    分类:技术分享    orm   ponyorm

How can I store a Python Enum using Pony ORM?

问题 假设我在这里有这个简单的小 Pony ORM 映射。 内置的 Enum 类是 Python 3.4 的新内容,并向后移植到 2.7。 from enum import Enum from pony.orm import Database, Required class State(Enum): ready = 0 running = 1 errored = 2 if __name__ == '__main__': db = Database('sqlite', ':memory:', create_db=True) class StateTable(db.Entity): state = Required(State) db.generate_mapping(create_tables=True) 当我运行程序时,会引发错误。 TypeError: No database converter found for type <enum 'State'> 这是因为 Pony 不支持映射枚举类型。 当然,这里的解决方法是只存储 Enum 值,并在 Class StateTable 中提供一个 getter 以再次将值转换为 Enum。 但这是乏味且容易出错的。 我也可以只使用另一个 ORM。 如果这个问题变得太令人头疼,也许我会。 但如果可以的话,我宁愿坚持使用 Pony。

2022-05-11 12:39:14    分类:技术分享    python   python-3.x   orm   enums   ponyorm

Pony ORM reports record "was updated outside of current transaction" while there is not other transaction

问题 代码很简单,如下: from pony.orm import Required, Set, Optional, PrimaryKey from pony.orm import Database, db_session import time db = Database('mysql', host="localhost", port=3306, user="root", passwd="123456", db="learn_pony") class TryUpdate(db.Entity): _table_ = "try_update_record" t = Required(int, default=0) db.generate_mapping(create_tables=True) @db_session def insert_record(): new_t = TryUpdate() @db_session def update(): t = TryUpdate.get(id=1) print t.t t.t = 0 print t.t if __name__ == "__main__": insert_record() update() pony.orm 报告异常:pony.orm.core.CommitException:对象 TryUpdate[1]

2022-05-06 17:29:16    分类:技术分享    python   orm   transactions   ponyorm

How can I store a Python Enum using Pony ORM?

Say I've got this simple little Pony ORM mapping here. The built-in Enum class is new as of Python 3.4, and backported to 2.7. from enum import Enum from pony.orm import Database, Required class State(Enum): ready = 0 running = 1 errored = 2 if __name__ == '__main__': db = Database('sqlite', ':memory:', create_db=True) class StateTable(db.Entity): state = Required(State) db.generate_mapping(create_tables=True) When I run the program, an error is thrown. TypeError: No database converter found for type <enum 'State'> This happens because Pony doesn't support mapping the enum type. Of course, the

2022-05-02 01:33:20    分类:问答    python   python-3.x   orm   enums   ponyorm

Pony ORM reports record "was updated outside of current transaction" while there is not other transaction

The code is quite simple, as follows: from pony.orm import Required, Set, Optional, PrimaryKey from pony.orm import Database, db_session import time db = Database('mysql', host="localhost", port=3306, user="root", passwd="123456", db="learn_pony") class TryUpdate(db.Entity): _table_ = "try_update_record" t = Required(int, default=0) db.generate_mapping(create_tables=True) @db_session def insert_record(): new_t = TryUpdate() @db_session def update(): t = TryUpdate.get(id=1) print t.t t.t = 0 print t.t if __name__ == "__main__": insert_record() update() pony.orm reports exception: pony.orm.core

2022-04-29 19:50:37    分类:问答    python   orm   transactions   ponyorm

PonyORM:在不知道哪些项目已经存在的情况下,向小马数据库添加新项目的最有效方法是什么?(PonyORM: What is the most efficient way to add new items to a pony database without knowing which items already exist?)

问题 如果这是一个明显的问题,请原谅我,但我对小马和数据库一般来说是新手,并且没有找到回答这个问题的文档的正确部分。 我正在尝试创建一个包含公司以及这些公司设有办事处的位置的数据库。 这是一种多对多的关系,因为每个公司位于多个位置,并且每个位置都可以托管多个公司。 我这样定义我的实体: from pony import orm class Company(db.Entity): '''A company entry in database''' name = orm.PrimaryKey(str) locations = orm.Set('Location') class Location(db.Entity): '''A location for a company''' name = orm.PrimaryKey(str) companies = orm.Set('Company') 理想情况下,我希望能够编写一个函数,将公司添加到数据库中,同时添加该公司存在的位置列表,同时确保添加新的位置实例(如果它们尚不存在)。 我可以很快想到两种方法。 首先是尝试输入该位置,即使它存在并处理异常: @orm.db_session def add_company(name, locations): loc_entities = [] for l in locations: try: loc

2022-01-14 07:03:56    分类:技术分享    python   relational-database   ponyorm

PonyORM: What is the most efficient way to add new items to a pony database without knowing which items already exist?

Forgive me if this is an obvious question but I'm new to pony and databases in general and didn't find the right part of the documentation that answers this question. I'm trying to create a database with companies and the locations where those companies have offices. This is a many-to-many relationship since each company is in multiple locations and each location can be host to multiple companies. I'm defining my entities as such: from pony import orm class Company(db.Entity): '''A company entry in database''' name = orm.PrimaryKey(str) locations = orm.Set('Location') class Location(db.Entity)

2022-01-08 20:23:55    分类:问答    python   relational-database   ponyorm

PonyORM - 多个模型文件(PonyORM - multiple model files)

问题 我想将我的模型类分成模型目录中的单独文件。 我想要一个单独的文件: 一般(身份验证和全局类/表) 申请表(用于申请表) 工作订单(用于工作订单的表格) sales_orders(用于销售订单的表) ...等等 我不确定如何构建我的项目来实现这一目标。 我尝试将我的主要导入放入目录中的init .py 并将它们导入到各个模型文件中,但我不知道将 db.generate_mapping() 放在哪里,以便所有类都可用。 我猜这是大型应用程序的最佳实践。 此时我的应用程序中有大约 150 个表。 任何帮助/指针将不胜感激。 回答1 您可以使用以下项目结构: # /myproject # settings.py # main.py # /models # __init__.py # base.py # requisitions.py # workorders.py # sales_orders.py settings.py是一个包含数据库设置的文件: # settings.py db_params = {'provider': 'sqlite', 'filename': ':memory:'} main.py是启动应用程序时的文件。 你把db.generate_mapping放在这里: # main.py from pony import orm import settings

2021-12-23 07:01:36    分类:技术分享    python   ponyorm

PonyORM - multiple model files

I want to separate my model classes into separate files in a models directory. I would like to have a separate file for: general (authentication and global classes/tables) requisitions (tables used for requisitions) workorders (tables used for workorders) sales_orders (tables used for sales orders) ...etc I'm not sure how to structure my project to make that happen. I've tried putting my main imports into init.py in the directory and them importing those into the individual model files, but I don't know where to put my db.generate_mapping() so that all classes are available. I'm guessing this

2021-12-11 13:26:13    分类:问答    python   ponyorm