1

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)
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

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.

2
  • Try using Table and mapper directly, instead of using declarative this way. Commented Jun 25, 2011 at 13:49
  • You must have bypassed the Base metaclass (DeclarativeMeta) when doing the type instanciation. Metaclasses are tricky to get right, happily they're not needed in this case (or most cases). Commented Jun 26, 2011 at 23:20

2 Answers 2

2

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 )
Sign up to request clarification or add additional context in comments.

Comments

1

This problem is caused by lack of instruments function interfaces for the orm as the error description says. And it is actually caused by self.__dict__ = dict(keyargs) I think. So this can be solved by reconstruct the init, which do not modify the injected functions by ORM.

Turn this

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

To

#my init function
def tableinit(self,**kwargs):
    self.__dict__.update(kwargs)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.