Forgive me if this has been asked before, but I can't seem to find the
answer. I'm trying to create a set of objects (Request and Response)
that have a base object of Packet. The Packet table contains the
general attributes such as the type and subtype whereas the Request
and Response tables include the specific information. The code for
this is shown below. The idea is that every product has a request,
however every request may have multiple responses. In addition, all
packets, whether a request or response, is available via the Product's
packets attribute.

I thought that I had this working, but then realized that I wasn't
able to query the db for any response objects. Additional testing
indicated that I am able to add products, requests, and responses to
the database, but as soon as I try to interact with a response object,
I get a TypeError exception saying that id() takes exactly one
argument. (A full trace is listed below the code.)  I'm guessing that
the error has to do with Response having multiple Foreign Keys to the
Packets object, but I am not sure. Any help would be greatly
appreciated. Thanks in advance.

Base = declarative_base()

class Product(Base):
    __tablename__ = 'products'

    id          = Column(Integer, primary_key=True)
    name        = Column(String)

    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return "<Product(%s)>" % (self.name)

class Packet(Base):
    __tablename__ = 'packets'

    id          = Column(Integer, primary_key=True)
    product_id  = Column(Integer, ForeignKey('products.id'))
    type        = Column('type', String)

    __mapper_args__ = {'polymorphic_on': type}

    product = relation(Product, uselist=False, backref=backref
('packets'))

    def __init__(self, product):
        self.product = product

    def __repr__(self):
        return "<Packet(%s, %s)>" % (self.type, self.subtype)

class Request(Packet):
    __tablename__ = 'requests'
    __mapper_args__ = {'polymorphic_identity': 'request'}

    id          = Column(Integer, ForeignKey('packets.id'),
primary_key=True)
    request     = Column(String)

    def __init__(self, product, request):
        Packet.__init__(self, product)
        self.request = request

    def __repr__(self):
        return "<Request(%s)>" % (self.request)

class Response(Packet):
    __tablename__ = 'responses'
    __mapper_args__ = {'polymorphic_identity': 'response',
'inherit_condition':id==Packet.id}

    id          = Column(Integer, ForeignKey('packets.id'),
primary_key=True)
    req_id      = Column(Integer, ForeignKey('packets.id'))
    response    = Column(String)

    request     = relation(Request, primaryjoin=req_id==Request.id,
uselist=False)

    def __init__(self, product, request, response):
        Packet.__init__(self, product)
        self.response = response

    def __repr__(self):
        return "<Response(%s)>" % (self.response)


resp = session.query(Response).first()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.6/site-packages/SQLAlchemy-0.5.6-py2.6.egg/
sqlalchemy/orm/query.py", line 1300, in first
    ret = list(self[0:1])
  File "/Library/Python/2.6/site-packages/SQLAlchemy-0.5.6-py2.6.egg/
sqlalchemy/orm/query.py", line 1221, in __getitem__
    return list(res)
  File "/Library/Python/2.6/site-packages/SQLAlchemy-0.5.6-py2.6.egg/
sqlalchemy/orm/query.py", line 1361, in __iter__
    return self._execute_and_instances(context)
  File "/Library/Python/2.6/site-packages/SQLAlchemy-0.5.6-py2.6.egg/
sqlalchemy/orm/query.py", line 1364, in _execute_and_instances
    result = self.session.execute(querycontext.statement,
params=self._params, mapper=self._mapper_zero_or_none())
  File "/Library/Python/2.6/site-packages/SQLAlchemy-0.5.6-py2.6.egg/
sqlalchemy/orm/session.py", line 755, in execute
    clause, params or {})
  File "/Library/Python/2.6/site-packages/SQLAlchemy-0.5.6-py2.6.egg/
sqlalchemy/engine/base.py", line 824, in execute
    return Connection.executors[c](self, object, multiparams, params)
  File "/Library/Python/2.6/site-packages/SQLAlchemy-0.5.6-py2.6.egg/
sqlalchemy/engine/base.py", line 872, in _execute_clauseelement
    parameters=params
  File "/Library/Python/2.6/site-packages/SQLAlchemy-0.5.6-py2.6.egg/
sqlalchemy/engine/base.py", line 938, in __create_execution_context
    return dialect.execution_ctx_cls(dialect, connection=self,
**kwargs)
  File "/Library/Python/2.6/site-packages/SQLAlchemy-0.5.6-py2.6.egg/
sqlalchemy/engine/default.py", line 167, in __init__
    self.compiled_parameters = [compiled.construct_params(m) for m in
parameters]
  File "/Library/Python/2.6/site-packages/SQLAlchemy-0.5.6-py2.6.egg/
sqlalchemy/sql/compiler.py", line 243, in construct_params
    pd[self.bind_names[bindparam]] = bindparam.value()
TypeError: id() takes exactly one argument (0 given)

--

You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.


Reply via email to