Search code examples
pythonponyorm

Return None as classtype in entity inheritance on PonyORM


During I learned about entity inheritance on PonyORM, I implemented the following due to my curiosity:

from pony.orm import *


db = Database()


class Person(db.Entity):
    classtype = Discriminator(str)
    name = Required(str)
    PrimaryKey(classtype, name)


class Student(Person):
    professors = Set('Professor')


class Professor(Person):
    students = Set('Student')


db.bind("sqlite", "entity.sqlite", create_db=True)
db.generate_mapping(create_tables=True)

with db_session:
    student_tom = Student(name='tom')
    print(student_tom)
print(student_tom)

with db_session:
    tom = Person['Student', 'tom']
    print(tom)
print(tom)

Output of my expectation is the following:

Student['Student',u'tom']
Student['Student',u'tom']
Student['Student',u'tom']
Student['Student',u'tom']

But, the actual result is the following:

Student['Student',u'tom']
Student['Student',u'tom']
Student[None,u'tom']
Student[None,u'tom']

I tested it on interactive console such as IPython and run as an python file. But, both results are same. It is tested on Python 2.7.10 and Python 3.6.0.

Originally, what I wanted to do is

  • make name and classtype pair as a unique identifier for person.

For example, it should be different between Tom as a professor and Tom as a student.

I thought it is related to different db_session problem but in that case, how can I retrieve Person['Student', 'tom'] or Student[None, 'tom']? Do they have different way to make classtype and name as a PrimaryKey?

Can somebody tell me what do I need to try? Did I miss something?

p.s., I have tested it on sqlite and PostgresSQL 9.5.1.


Solution

  • Thanks for asking, it was a bug and I fixed it.

    We didn't discover it earlier, because typically discriminator column is not included into primary key. Primary key should contain unique values, and discriminator column value is the same for all objects of the same type.