Hi all,
my class Clients inherits from BaseApparati
and a client can have multiple relations with others clients.
I use the class ClientCrossRef as table for the relation.


class BaseApparati(Base, Dictionaryable):
        __tablename__ = 'baseapparati'
        id = Column(Integer, Sequence((__tablename__+'id_seq')[-30:]),
autoincrement=True, primary_key=True)
        realtype = Column(Unicode(30), nullable=False, index=True)

        __mapper_args__ = {'polymorphic_on': realtype,
'polymorphic_identity': 'baseapparati'


class Clients(BaseApparati):
        __tablename__ = 'clients'
        __mapper_args__ = {'polymorphic_identity': 'client'}

        id = Column(Integer, ForeignKey('baseapparati.id'), primary_key=True)


class ClientCrossRef(Base):
        __tablename__ = 'clientcrossref'

        id = Column(Integer, Sequence((__tablename__+'id_seq')[-30:]),
autoincrement=True, primary_key=True)
        master_id = Column(Integer, ForeignKey('clients.id'))
        slave_id = Column(Integer, ForeignKey('clients.id'))

        master = relation(Clients, uselist=False, primaryjoin= master_id ==
Clients.id, foreign_keys = Clients.id)
        slave = relation(Clients, uselist=False, primaryjoin= slave_id ==
Clients.id, foreign_keys = Clients.id)

        def __init__(self, master, slave):
                self.master = master
                self.slave = slave



When i try to commit a new ClientCrossRef, SQLAlchemy seems not to
know the objects i passed to the constructor.

Example:

>>>  v= sa.Session.query(sa.Clients).all()
>>>  v[0]
        <Client('client_1')>
>>>  v[1]
        <Client('client_2')>

so v[0] and v[1] are instances of Clients model


>>>  sa.Session.add(  sa.ClientCrossRef( v[0], v[1] )  )
>>>  sa.Session.commit()

this is the sql genrated:

INSERT INTO clientcrossref (id, master_id, slave_id) VALUES
(:id, :master_id, :slave_id)
{'master_id': None, 'id': 3, 'slave_id': None}

as you can see, master_id and slave_id are == None, so it raise an
exception.


If i change the constructor of ClientCrossRef to accept the id of the
object (not the object!)
it obviously works:

        def __init__(self, master_id, slave_id):
                self.master_id = master_id
                self.slave_id = slave_id

>>>  sa.Session.add( sa.ClientCrossRef( v[0].id, v[1].id) )
>>>  sa.Session.commit()

INSERT INTO clientcrossref (id, master_id, slave_id) VALUES
(:id, :master_id, :slave_id)
{'master_id': 1, 'id': 3, 'slave_id': 2}



So i think the problem is the definition of the relations 'master' and
'slave'.
Can anyone help me? I need to pass objects to the constructor, not the
ids.

Thanks


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