Trying quite the same solution I got a different error:
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
Il 29/12/2010 18:12, Bruno Rocha ha scritto:
Maybe not the best way, but just for testing, I tried this:
<model>
db.define_table('person',
Field('unikey','id',compute=lambda r: r.fname+r.lname),
Field('fname'),
field('lname')
)
</model>
got an error because the 'id' needs to be int
<error>
File "/home/rochacbruno/projects/web2py/gluon/dal.py", line 805, in expand
return self.represent(expression,field_type)
File "/home/rochacbruno/projects/web2py/gluon/dal.py", line 1096, in
represent
return str(int(obj))
ValueError: invalid literal for int() with base 10:
</error>
Then I tried with integers and works ok.
person.unikey = 12
person.fname = 1
person.lname = 2
Is there a way to have string fields as primary keys?