2
在我上PonyORM了解entity inheritance,我采取了以下由于我的好奇:返回None作为CLASSTYPE在实体继承上PonyORM
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)
我的期望的输出如下:
Student['Student',u'tom']
Student['Student',u'tom']
Student['Student',u'tom']
Student['Student',u'tom']
但是,实际结果如下:
Student['Student',u'tom']
Student['Student',u'tom']
Student[None,u'tom']
Student[None,u'tom']
我测试了它在交互式控制台(如IPython)上运行,并作为python文件运行。但是,两个结果都是一样的。它在Python 2.7.10和Python 3.6.0上进行了测试。
本来就是我想要做的是
- 化妆名和CLASSTYPE对作为人的唯一标识。
例如,作为教授的汤姆和作为学生的汤姆应该不同。
我以为它涉及到不同的db_session问题,但在这种情况下,我如何检索Person['Student', 'tom']
或Student[None, 'tom']
?他们有不同的方式使classtype
和name
作为PrimaryKey吗?
有人可以告诉我该怎么做?我错过了什么?
p.s.我已经在sqlite和PostgresSQL 9.5.1上测试过它。
感谢您修复它。我测试了你的补丁,效果很好。即使我固定了相同的部分,它运作良好,但尚未为项目公关。如果我找到另一个,我会在下一次做。谢谢。 我同意你的观点在鉴别器列作为主键的一部分。所以,我试图改变我的模型,但我还没有找到更好的方式。 :) – sangheestyle