我需要更改实体中主键的原始值,但是我无法这样做。例如:
#!/usr/bin/env python3
# vim: set fileencoding=utf-8
from pony import orm
db = orm.Database("sqlite", ":memory:", create_db=True)
class Car(db.Entity):
number = orm.PrimaryKey(str, 12)
owner = orm.Required("Owner")
class Owner(db.Entity):
name = orm.Required(str, 75)
cars = orm.Set("Car")
db.generate_mapping(create_tables=True)
with orm.db_session:
luis = Owner(name="Luis")
Car(number="DF-574-AF", owner=luis)
with orm.db_session:
car = Car["DF-574-AF"]
# I try to change the primary key
car.set(number="EE-12345-AA")但是我得到了一个TypeError (不能改变主键属性号的值)。
发布于 2018-03-13 20:00:56
理想情况下,主键应该是不可变的。您可以将自动增量id作为主键添加到您的Car类中,然后使您的number具有唯一性,您将能够轻松地更改它,同时仍然具有相同的约束。
例如:
class Car(db.Entity):
id = PrimaryKey(int, auto=True)
number = orm.Required(str, 12, unique=True)
owner = orm.Required("Owner")https://stackoverflow.com/questions/44482132
复制相似问题