Unfortunately there is no simple way to achieve multi-column unique
costraints in web2py.
Aside from your implementation, you can use a third column that stores
a composite key, say "%s_$$$_%s" % (key1, key2), that you can compute
and set as unique.

Something like (not tested):

db.define_table('t9n_mytable',
 Field('record', 'references records_table'),
 Field('language', length=2, notnull=True),
 Field('unique_col_costraint',
       compute=lambda row: "%s_$$$_%s" % (row['record'],
row['language']),
       unique=True)
)


On 20 Nov, 15:17, ~redShadow~ <[email protected]> wrote:
> I just developed a way to quickly handle translations of table records,
> that works this way:
>
> Assuming we have a table name 'mytable' with three fields ('field0',
> 'field1' and 'field2'), it creates an exact copy of the original table
> (named 't9n_mytable', containing all the original fields plus a
> 'language' field, containin language code, and a 'record' field that is
> a reference to mytable).
>
> Now, I want to be sure there are no duplicate translations, and way to
> do so would be to create an unique index on ``(record, language)``, but
> it looks like there is no straight-forward way to do that in web2py..
>
> For the moment, I solved using this code, but it doesn't look great.. is
> there a better way to do this?
>
> Code::
>
>     _table_existed = ('t9n_mytable' in db.tables)
>
>     ## Here, define the table...
>
>     if not _table_existed:
>         import urlparse
>         dbtype = urlparse.urlparse(db._uri).scheme
>         if dbtype == 'sqlite':
>             db.executesql('CREATE UNIQUE INDEX'
>                 ' IF NOT EXISTS unique_record_language'
>                 ' ON t9n_mytable (record, language);')
>         elif dbtype == 'mysql':
>             db.executesql('CREATE UNIQUE INDEX'
>                 ' unique_record_language'
>                 ' ON t9n_mytable (record, language);')
>         elif dbtype == 'pgsql':
>             db.executesql('CREATE UNIQUE INDEX'
>                 ' unique_record_language'
>                 ' ON t9n_mytable (record, language);')
>         else:
>             pass
>
> --
> Samuele ~redShadow~ Santi
> ----------------------------------------------------------------
>      redshadow[at]hackzine.org - redshadowhack[at]gmail.com
>
>   Blog:http://hackzine.org
>
>   GPG Key signature:
>        050D 3E9F 6E0B 44CE C008 D1FC 166C 3C7E EB26 4933
> ----------------------------------------------------------------
> /me recommends:
>     Squadra Informatica -http://www.squadrainformatica.com
> ----------------------------------------------------------------
>  - Proud ThinkPad T-Series owner
>  - Registered Linux-User: #440008
>       * GENTOO User since 1199142000 (2008-01-01)
>       * former DEBIAN SID user
> ----------------------------------------------------------------
>       "Software is like sex: it's better when it's free!"
>                               -- Linus Torvalds
>
>  signature.asc
> < 1KVisualizzaScarica

Reply via email to