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

Attachment: signature.asc
Description: This is a digitally signed message part

Reply via email to