[orm-devel] Question: setting an object's many2one attribute
to a "None" value
Michael Watkins
orm-devel@mailman.tux4web.de
Sun, 31 Aug 2003 16:20:34 -0700
Works as it should - thanks, I'll back out my own hack and use your
code.
PS, is there anything in CVS that ought to prevent a Postgres instance
of ORM from working? Just thought I'd ask before I start heading down
the path to understand what I saw last night on a (very) quick test of
CVS ORM.
Mike
On Sun, 2003-08-31 at 11:25, Diedrich Vorberg wrote:
> Hi Mike,
>
> >Simply setting
> >
> > product_instance.type_id = None
> >
> >Will fail of couse, since the class expects a dbobj on the right hand
> >side, not None.
>
> Actually this is not an "of course", it's a bug in ORM! The case of a
> many2one relationship being set to None (which should set the
> corresponding forign key column in your product table to NULL) is not
> handled by ORM!
>
> Here's a new version of many2oneColumn's set() method. I didn't test
> this, sorry...
>
> Diedrich
>
> ----------------------------------------------------------------------
>
> class many2oneColumn(_2oneColumn):
> def set(self, data):
> """
> You can only assign objects to a many2one-managed attribute
> which are of the right type and have already been inserted
> into the database.
> """
> if data is None:
> self.ds().update(self._dbObj, self._columnName, "NULL")
> if self._columnName != self._attributeName and \
> self._dbObj.columns.has_key(self._columnName):
> self._dbObj.__setattr__(self._columnName, None)
> else:
> if not isinstance(data, self._childClass):
> raise ObjectMustBeInserted(
> "You can only assign objects of %s to this column" \
> % self._childClass.__name__)
>
> if not data.oid():
> raise ObjectMustBeInserted(
> "You must insert a dbobj before you assign it to a " + \
> "one2one relationship")
>
> if self._dbObj.oid():
> fmt = data.primaryKeyValue().format()
> self.ds().update(self._dbObj, self._columnName, fmt)
>
> # if there is a column Object representing the foreign key column
> # set it to the value of the newly asigned object
> if self._columnName != self._attributeName and \
> self._dbObj.columns.has_key(self._columnName):
> self._dbObj.__setattr__(self._columnName,
> data.primaryKeyValue().get())
> self._retrieved = 1
> self._data = data
>
>