I am probably asking an obvious thing, but I could not find the answer. I am learning Pony ORM, and started with creating simple database from the documentation. I made a design of a simple database diagram and code https://editor.ponyorm.com/user/nidza11/Ponz#
from pony.orm import *
db = Database()
class Person(db.Entity):
id = PrimaryKey(int, auto=True)
name = Required(str)
age = Required(int)
cars = Set('Car', cascade_delete=True)
class Car(db.Entity):
id = PrimaryKey(int, auto=True)
make = Required(str)
model = Required(str)
person = Required(Person)
db.bind("sqlite", "TestDB.sqlite", create_db = True)
db.generate_mapping(create_tables=True)
with db_session:
p1 = Person(name="John", age=24)
p2 = Person(name="Ive", age=26)
p3 = Person(name="Olivia", age = 26)
c1 = Car(make="Toyota", model = "Prius", person = p2)
p3 = Car(make="Ford", model = "Explorer", person = p3)
After running the code, database was created and populated.
I would like to make a name attribute in Person table unique. So I made a change in code
name = Required(str, unique=True)
After running the code again database was populated, but the name field is not set unique. Data was duplicated. According to DB browser name field was 'unique'. I expected an error. Such error will help me keep entity and database synchronized.
Adding a new attribute only in Person table class, but not in database, will cause an error.
Is it possible to make a change only in one place inside the code, that will reflect in database as well?
If I have to make a same change on two places, how to do it nice and clean?
I've just asked on the Pony ORM 'Telegram' group and this is the transaction that occurred there.