On Mon, 2012-07-30 at 19:56 -0400, Michael Bayer wrote: 
> On Jul 30, 2012, at 7:47 PM, Adam Tauno Williams wrote:
> > I have two classes Contact and CompanyInfo that should exist in a 1:1
> > relation; CompanyInfo contains a comment related to Contact.  This works
> > - .... -
> > Is there anyway to get the SQLalchemy model to automatically create a
> > CompanyInfo object if one is missing?
> A much quicker way would be to just make "text" into a @property that
> returns "self._text", and the association prox would be returning
> "_text_. It could do the job of sticking a "CompanyInfo" on the
> object. Downside of this is, a little bit of method latency.
> But really, if you're going to make a @property, and this is a scalar
> relationship anyway, I'm not sure what the win of assocation_proxy is
> in the first place, just do a descriptor directly:

Yes, this makes sense.  I've refactored in the way you suggested and all
the tests pass - including the ones for the 'broken' pairs.  Thanks.

> class Contact(...):
> # ...
> 
> @property
> def comment(self):
> if self._info is None:
> self._info = CompanyInfo(text="")
> return self._info.text
> 
> @comment.setter
> def comment(self, value):
> if self._info is None:
> self._info = CompanyInfo(text=value)
> else:
> self._info.text = value

Attachment: signature.asc
Description: This is a digitally signed message part

Reply via email to