[orm-devel] Question: setting an object's many2one attribute to a "None" value

Diedrich Vorberg orm-devel@mailman.tux4web.de
Sun, 31 Aug 2003 20:25:00 +0200


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    



-- 
           _..._                            Diedrich Vorberg
         .'     '.
        /  _   _  \                         http://www.tux4web.de
        | (o)_(o) |                         info@tux4web.de
         \(     ) /            .---.
         //'._.'\ \           /     \       Internet Dienstleistungen
        //   .   \ \          \.@-@./       und 'Consulting'.
       ||   .     \ \         /`\_/`\
       |\   :     / |        //  _  \\      Linux Rules!
       \ `) '   (`  /_      | \     )|_
     _)``".____,.'"` (_    /`\_`>  <_/ \
     )     )'--'(     (    \__/'---'\__/
      '---`      `---`