動的クラスを使用してテーブルを作成するログシステムを作成しようとしています。クラスを作成すると、作成されたテーブルは正常に動作しているようですが、エントリを入力しようとするとマッピングに関するエラーメッセージが表示されます。サンプルコードとエラーメッセージは次のとおりです。動的クラスと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.
'Table'と'、直接mapper'代わりにこの方法を使用して、宣言のを使用してみてください。 –
タイプインスタンシエーションを実行するときは、ベースメタクラス(DeclarativeMeta)をバイパスする必要があります。メタクラスは正しくなるには手間がかかります。幸いなことに、このケースでは(ほとんどの場合)必要ありません。 – Tobu