Type error when using SQLAlchemy and Python

Nathan Harmston ratchetgrid at googlemail.com
Mon Jun 11 12:56:46 CEST 2007


HI,

I posted this to sqlalchemy but didnt get a response, so I was
wondering if anyone on python-list could help me.

I m currently trying to build an api for a database and I really like
the way that Djangos manager ( Class_name.objects ) is set up. This
seems very intuitive for me. After reading through the some of the
django source and getting slightly confused I ve implemented a basic
version for one of my tables. The problem is I get an exception, when
I m not expecting one.

registry.py --> contains table definitions etc.

Manager.py

from Registry import *

class Manager(object):
    def __init__(self, model, table=None):
        self.model = model
        self.table = table
    def get(self, slice):
        pass
    def all(self):
        print "1"
        mapper(self.model, interpro_table)
        print "2"
        session = create_session()
        print "3"
        query = session.query(self.model)
        print "4"
        return query.select()

Models.py

from Manager import *

class ModelBase(type):
    def __new__(cls, name, bases, dict):
        print cls
        setattr(cls, 'objects', Manager(cls))
        return type.__new__(cls, name, bases, dict)

class Model(object):
    __metaclass__=ModelBase

class InterPro(Model):
    _tableName = interpro_table
    def __init__(self, interpro_ac):
        self.interpro_ac = interpro_ac
    def __str__(self):
        return "InterPro: %s" %(self.interpro_ac)
    def __repr__(self):
        return "InterPro: %s" %(self.interpro_ac)

if __name__=='__main__':
    a = Manager(InterPro)
    print a
    print a.all() --> this prints out all of the objects in the database
    i = InterPro('IPR014697')
    print InterPro.objects
    print InterPro.objects.all()
 --> this fails and produces the exception.

Traceback (most recent call last):
  File "Model.py ", line 28, in ?
    print InterPro.objects.all()
  File "/home/skeg/workspace/test/src/Manager.py", line 17, in all
    return query.select()
  File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/query.py",
line 247, in select
    return self.select_whereclause(whereclause=arg, **kwargs)
  File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/query.py",
line 252, in select_whereclause
    return self._select_statement(statement, params=params)
  File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/query.py",
line 378, in _select_statement
    return self.execute(statement, params=params, **kwargs)
  File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/query.py",
line 310, in execute
    return self.instances(result, **kwargs)
  File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/query.py",
line 329, in instances
    self.mapper._instance(context, row, result)
  File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/mapper.py",
line 1213, in _instance
    instance = self._create_instance(context.session)
  File "/usr/lib/python2.4/site-packages/sqlalchemy/orm/mapper.py",
line 1234, in _create_instance
    obj = self.class_.__new__(self.class_)
TypeError: __new__() takes exactly 4 arguments (1 given)

Does anyone know what the problem is? Is it the way I ve programmed
this using metaclasses or is it sqlalchemy and the way in instantiates
objects or even both?

Many Thanks in advance,



More information about the Python-list mailing list