Logick Logick - 1 year ago 85
Python Question

Error with dynamic classes and sqlalchemy

I am trying to write a logging system, which uses dynamic classes to make tables. Getting the classes created, and the tables created seems to be working fine, but trying to put entries into them is lead to an error message regarding mapping, below is the sample code and the error message.

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)

#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

The error:

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.

Answer Source

This is easier if you create a function to return a class that you set up normally. I've tried something like this and it works:

def getNewTable( db, table ):
    class NewTable( Base ):
        __tablename__ = table
        __table_args__ = { 'schema': db }
        id = Column( ...

    return NewTable

newClassInst = getNewTable( 'somedb', 'sometable' )
newRow = newClassInst( data )
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download