I found that it is essential to first point the parent to the child
and only then set
the back reference from in the child object to the parent. Otherwise
the ORM goes into non-ending
recursion.

On Sep 12, 3:52 pm, Ernst <[email protected]> wrote:
> Hi,
> I do not understand the backref bit. With the setup as in the previous
> reply the Applicant object can use anApplicant._state to get the State
> object.
> How can the State object get the related Applicant? Is a change in the
> mapping needed?  aState.applicant (as in backref) leads to
>   File "/usr/lib/pymodules/python2.5/sqlalchemy/orm/attributes.py",
> line 588, in set
>     value = self.fire_replace_event(state, dict_, value, old,
> initiator)
>   File "/usr/lib/pymodules/python2.5/sqlalchemy/orm/attributes.py",
> line 608, in fire_replace_event
>     value = ext.set(state, value, previous, initiator or self)
>   File "/usr/lib/pymodules/python2.5/sqlalchemy/orm/attributes.py",
> line 849, in set
>     initiator, passive=PASSIVE_NO_FETCH)
>   File "/usr/lib/pymodules/python2.5/sqlalchemy/orm/attributes.py",
> line 394, in remove
>     self.set(state, dict_, None, initiator, passive=passive)
>   File "/usr/lib/pymodules/python2.5/sqlalchemy/orm/attributes.py",
> line 584, in set
>     old = self.get(state, dict_)
> RuntimeError: maximum recursion depth exceeded
>
> Regards
> Ernst
>
> On Sep 11, 1:50 am, Ernst <[email protected]> wrote:
>
> > Made two changes:
>
> > applicant_table = Table('applicant', metadata,
> >   Column('id',Integer, primary_key=True),  # not in model
> >   Column('state', Integer, ForeignKey('applicantstate.id')),
> > )
>
> > and
>
> > mapper(Applicant.Applicant, applicant_table, properties={
> >   '_state':relation(Applicant.State, backref=backref('applicant',
> > uselist=False))
>
> > })
>
> > and it does what I wished for. Being new I have to learn the basics:
> > state is a foreign key in the database which the orm writes and _state
> > is an attribute in the object which the orm 'assigns' on retrieval.
>
> > Thank you
> > Ernst
>
> > On Sep 11, 12:39 am, Michael Bayer <[email protected]> wrote:
>
> > > On Sep 10, 2010, at 3:14 AM, Ernst wrote:
>
> > > > Hi,
> > > > I have manged to set up joined and single table inheritance mappings.
> > > > When relationships get involved I need help.
>
> > > > I would like to use the state pattern. Class Applicant delegates to a
> > > > subclass of State.
>
> > > > applicant_table = Table('applicant', metadata,
> > > >  Column('applicant_id',Integer, primary_key=True),
> > > >  Column('_state', Integer, ForeignKey('applicantstate.state_id')),
> > > > )
>
> > > > appstate_table = Table('applicantstate', metadata,
> > > >  Column('state_id',Integer, primary_key=True),
> > > >  Column('type', String(30),nullable=False),
> > > >  Column('_givenName', String(40))
> > > > )
>
> > > > # applicant - state relationship, one to one, SQLAlchemy-0.5.8-
> > > > #mapper(Applicant, applicant_table, properties={
> > > > #  'applicantstate':relation(State, uselist=False,
> > > > backref='applicant')
> > > > #})
>
> > > > mapper(Applicant, applicant_table, properties={
> > > >  'applicantstate':relation(State, backref=backref('applicant',
> > > > uselist=False))
> > > > })
>
> > > > I tried both relationships and get:
> > > > ProgrammingError: (ProgrammingError) can't adapt '
> > > > INSERT INTO applicant (applicant_id, _state)
> > > > VALUES (%(applicant_id)s, %(_state)s)'
> > > > {'_state': <tg21tut.model.Applicant2.NewStdApp object at 0x9034bec>,
> > > > 'applicant_id': 1L}
>
> > > > I have omitted the single table inheritance mappings.
>
> > > The relationship is valid. The error there arises from some inappropriate 
> > > manipulation of objects,  such as setting applicant._state = 
> > > SomeApplicantState(), instead of assigning to the "applicantstate" 
> > > attribute.    Full detail would be needed in order to determine the issue.

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

Reply via email to