2017-03-17 52 views
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']?他們有不同的方式使classtypename作爲PrimaryKey嗎?

有人可以告訴我該怎麼做?我錯過了什麼?

p.s.我已經在sqlite和PostgresSQL 9.5.1上測試過它。

回答

1

感謝提問,這是一個bug,我也是fixed吧。

我們之前沒有發現它,因爲鑑別器列通常不包含在主鍵中。主鍵應該包含唯一值,鑑別器列值對於相同類型的所有對象都是相同的。

+0

感謝您修復它。我測試了你的補丁,效果很好。即使我固定了相同的部分,它運作良好,但尚未爲項目公關。如果我找到另一個,我會在下一次做。謝謝。 我同意你的觀點在鑑別器列作爲主鍵的一部分。所以,我試圖改變我的模型,但我還沒有找到更好的方式。 :) – sangheestyle