Le jeudi 23 février 2006 à 18:26 +0200, Max Ischenko a écrit :
> I have an SQLObject model class like this:
> class ResourceAccessRule(SQLObject):
>     ...
>     action = ForeignKey('ResourceAction', notNone=True, title="Action")
>     resource = ForeignKey('Resource', notNone=True, title="Resource")
> 
> I used notNone=True to hint SQLObject that this field is required but in vain:
> 
> >>> ResourceAccessRule(action=None, resource=None)
>   File 
> "c:\python24\lib\site-packages\sqlobject-0.7.1dev_r1547-py2.4.egg\sqlobject\mysql\mysqlconnection.py",
>     return cursor.execute(query)
>   File "C:\Python24\lib\site-packages\MySQLdb\cursors.py", line 137, in 
> execute
>     self.errorhandler(self, exc, value)
>   File "C:\Python24\lib\site-packages\MySQLdb\connections.py", line 33, in 
> defaulterrorhandler
>     raise errorclass, errorvalue
> OperationalError: (1048, "Column 'action_id' cannot be null")
> 
> Is there a way to enforce non-None values in ctor?
> 
> So far I resorted to:
> 
>     def _create(self, id, **kw):
>         if not kw.get('action'):
>             raise ValueError('No action given')
>         if not kw.get('resource'):
>             raise ValueError('No resource given')
>         super(ResourceAccessRule, self)._create(id, **kw)
> 
> Is there a better way?
> 
> Thanks,
> Max.

The behaviour is the one you asked ...

You give the table action=None, it answers OperationalError: (1048,
"Column 'action_id' cannot be null"). That's logic.

If notNone=True, you need to specify an action. There is no better way
than checking the value of the action attribute before inserting.

Didrik

Attachment: signature.asc
Description: Ceci est une partie de message numériquement signée

Reply via email to