>>> db.define_table('mytable',
... Field('field1','integer'),
... Field('field2','integer'),
... )
>>> class MyVirtualFields(object):
... def uniqueField(self):
... d = dict()
... for i in ['field1', 'field2']:
... d[db.mytable[i].name] = self.mytable[i]
... return d
...
>>> db.mytable.virtualfields.append(MyVirtualFields())
>>> db.mytable.uniqueField.requires = IS_NOT_IN_DB(db,
'mytable.uniqueField')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/manuele/Progetti/web2py/gluon/sql.py", line 1688, in
__getattr__
return dict.__getitem__(self,key)
KeyError: 'uniqueField'
what's wrong in it?
thank you very much
Manuele
Traceback (most recent call last):
File "/home/manuele/Progetti/web2py/gluon/restricted.py", line 188, in
restricted
exec ccode in environment
File
"/home/manuele/Progetti/web2py/applications/wrf_dbi/models/wrfdb.py"
<http://localhost:8000/admin/default/edit/wrf_dbi/models/wrfdb.py>, line
109, in <module>
*genericFields+specificFields['wind']
File "/home/manuele/Progetti/web2py/gluon/sql.py", line 1365, in
define_table
t._create(migrate=migrate, fake_migrate=fake_migrate)
File "/home/manuele/Progetti/web2py/gluon/sql.py", line 1861, in _create
fake_migrate=fake_migrate)
File "/home/manuele/Progetti/web2py/gluon/sql.py", line 1934, in _migrate
self._db._execute(sub_query)
File "/home/manuele/Progetti/web2py/gluon/sql.py", line 983, in <lambda>
self._execute = lambda *a, **b: self._cursor.execute(*a, **b)
File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 166, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 35, in
defaulterrorhandler
raise errorclass, errorvalue
ProgrammingError: (1064, "You have an error in your SQL syntax; check
the manual that corresponds to your MySQL server version for the right
syntax to use near 'VARCHAR(512)' at line 1")
I used a string field as the unique key and the function called by
lambda returns a string.
Manuele
so it coud be a solution to manage uniques
bye
M.
Il 05/01/2011 12:04, Manuele Pesenti ha scritto:
> Trying quite the same solution I got a different error:
>
> [omissis]
def getUnique(**kwargs):
...
return out
now add this 'myid' field to your table as in the example below:
db.define_table('mytable',
Field('field1', ...),
Field('field2', ...),
...
Field('myid', length=256, unique=True,
compute=lambda r: getUnique(
**dict([(x, r[x]) for x in (field2, field3, ..., )])
)
I hope I've been clear
bye
Manuele