Hi Bruno,

Work was done (I believe by Denes) to introduce non-standard primary
keys. However, that was really for legacy DBs that could not be
migrated to the web2py conventions. In any case, it was only available
for certain DBs and I'm not sure the work has been migrated and tested
properly in the new DAL.

In the case of a new app,  you should always try to have standard
integer primary keys (excepting uids in the case of  combining tables/
databases).

I would suggest you try compute to produce a combined 'string' field
and then use IS_NOT_IN_DB. You might prefer to use all lower or upper
case to make it case-insensitive.

Alternatively you can create a unique index at the DB level.  You
would have to use some raw SQL which would vary according to the DB
you use.  That would stop duplicates effectively enough, but I guess
your web2py app would not handle it as smoothly.

-D

On Dec 29, 5:12 pm, Bruno Rocha <rochacbr...@gmail.com> wrote:
> 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?

Reply via email to