Thread: [SQLObject] how to enforce non-null FK value
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Max I. <isc...@gm...> - 2006-02-23 16:26:47
|
I have an SQLObject model class like this: class ResourceAccessRule(SQLObject): ... action =3D ForeignKey('ResourceAction', notNone=3DTrue, = title=3D"Action") resource =3D ForeignKey('Resource', notNone=3DTrue, = title=3D"Resource") I used notNone=3DTrue to hint SQLObject that this field is required but = in vain: >>> ResourceAccessRule(action=3DNone, resource=3DNone) File = "c:\python24\lib\site-packages\sqlobject-0.7.1dev_r1547-py2.4.egg\sqlobje= ct\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. |
From: Rick F. <rf...@im...> - 2006-02-23 16:30:16
|
You want "notNull=True" instead of "notNone=True". On Thu, 23 Feb 2006, Max Ischenko wrote: > > 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. > > > > ------------------------------------------------------- > This SF.Net email is sponsored by xPML, a groundbreaking scripting language > that extends applications into web and mobile media. Attend the live webcast > and join the prime developer group breaking into this new coding territory! > http://sel.as-us.falkag.net/sel?cmd_______________________________________________ > sqlobject-discuss mailing list > sql...@li... > https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss > > |
From: Max I. <isc...@gm...> - 2006-02-23 16:37:47
|
> -----Original Message----- > From: Rick Flosi [mailto:rf...@im...] > Sent: Thursday, February 23, 2006 6:30 PM > To: Max Ischenko > Cc: sql...@li... > Subject: Re: [SQLObject] how to enforce non-null FK value > > > You want "notNull=True" instead of "notNone=True". Indeed, much thanks! Feeling a bit embarrassed for overlooking this option. |
From: Didrik P. <dp...@it...> - 2006-02-23 16:33:23
|
Le jeudi 23 f=E9vrier 2006 =E0 18:26 +0200, Max Ischenko a =E9crit : > I have an SQLObject model class like this: > class ResourceAccessRule(SQLObject): > ... > action =3D ForeignKey('ResourceAction', notNone=3DTrue, title=3D"Acti= on") > resource =3D ForeignKey('Resource', notNone=3DTrue, title=3D"Resource= ") >=20 > I used notNone=3DTrue to hint SQLObject that this field is required but i= n vain: >=20 > >>> ResourceAccessRule(action=3DNone, resource=3DNone) > 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 e= xecute > self.errorhandler(self, exc, value) > File "C:\Python24\lib\site-packages\MySQLdb\connections.py", line 33, i= n defaulterrorhandler > raise errorclass, errorvalue > OperationalError: (1048, "Column 'action_id' cannot be null") >=20 > Is there a way to enforce non-None values in ctor? >=20 > So far I resorted to: >=20 > 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) >=20 > Is there a better way? >=20 > Thanks, > Max. The behaviour is the one you asked ... You give the table action=3DNone, it answers OperationalError: (1048, "Column 'action_id' cannot be null"). That's logic. If notNone=3DTrue, you need to specify an action. There is no better way than checking the value of the action attribute before inserting. Didrik |