2011-06-24 7 views
1

動的クラスを使用してテーブルを作成するログシステムを作成しようとしています。クラスを作成すると、作成されたテーブルは正常に動作しているようですが、エントリを入力しようとするとマッピングに関するエラーメッセージが表示されます。サンプルコードとエラーメッセージは次のとおりです。動的クラスとsqlalchemyのエラー

Base = declarative_base() 

#my init function 
def tableinit(self,keyargs): 
    self.__dict__ = dict(keyargs) 

#table creation 
tableName = "newTable" 
columnsDict["__tablename__"] = tableName 
columnsDict["__init__"] = tableinit 
columnsDict["id"] = Column("id",Integer, autoincrement = True, nullable = False, primary_key=True) 
columnsDict["pid"] = Column("pid",Integer, ForeignKey('someparenttable.id')) #someparenttable is created with a hard coded class 
newTable = type(tableName,(Base,),columnsDict) 
tableClassDict[tableName]=newTable 

#when doing an entry 
newClassInst = subEntryClassDict[tableName] 
newEntry = newClassInst(dataDict) 
entryList.append(newEntry) # this is called in a for loop with the entries for someparenttable's entries also 

self.session.add_all(entryList) # at this point the error occurs 

エラー:あなたが正常にセットアップしたクラスを返す関数を作成する場合

UnmappedInstanceError: Class 'newTable' is mapped, but this instance lacks instrumentation. This occurs when the instance is created before sqlalchemy.orm.mapper(module.newTable) was called.

+0

'Table'と'、直接mapper'代わりにこの方法を使用して、宣言のを使用してみてください。 –

+0

タイプインスタンシエーションを実行するときは、ベースメタクラス(DeclarativeMeta)をバイパスする必要があります。メタクラスは正しくなるには手間がかかります。幸いなことに、このケースでは(ほとんどの場合)必要ありません。 – Tobu

答えて

2

これは簡単です。私はこのようなものを試してみました。

1

この問題は、エラー記述にあるように、ormの計測器機能インターフェイスが不足していることが原因です。実際にはself.__dict__ = dict(keyargs)が原因だと思います。 これは、を再構築することで解決できます。これらは注入された機能をORMで変更しません。

電源を入れ、この

#my init function 
def tableinit(self,keyargs): 
    self.__dict__ = dict(keyargs) 

#my init function 
def tableinit(self,**kwargs): 
    self.__dict__.update(kwargs) 
関連する問題