2012-08-02 42 views
0

我有兩個表,該模式是這樣的:多態性遺傳與反思

Table Auth: 
id: Integer, PrimaryKey 
type: String 

Table Password_auth 
id: Integer, PrimaryKey, ForeignKey(Auth.id) 
password: String 

我使用加入SQLAlchemy的繼承來表示他們班以上兩個表。我使用類型作爲多態的鑑別器。只要我不使用反射,這一切都可以正常工作。只要我用反射,所有的地獄都會破裂。

能在SQLAlchemy中用反射使用多態連接繼承嗎?如果是這樣的話,有人會在給我一個例子說明它是如何做的時候謹慎地採取一些措施嗎?

在此先感謝。

回答

2

假設你正在使用declarative extension,下面的代碼應該給你一個想法:

class Auth(Base): 
    __table__ = Table('Auth', Base.metadata, 
      autoload=True, 
      autoload_with=engine) 
    __mapper_args__ = { 
      'polymorphic_on': 'type', 
      'polymorphic_identity': 'auth', 
      } 

class Password_Auth(Auth): 
    __table__ = Table('Password_Auth', Base.metadata, 
      autoload=True, 
      autoload_with=engine) 
    __mapper_args__ = { 
      'polymorphic_identity': 'password_auth', 
      } 
+0

感謝這個麪包車。 – 2012-08-05 20:11:06