Search code examples
web2py

Evaluate value in string-type field


I have the following code:

1) table "keys" with Field ('kcv', 'string')
2) grid = SQLFORM.grid(db.keys, user_signature=False)

When I'm trying to Edit or create a record with value 9E2 in a kcv field I get "900" in kcv field of a new record. How can I get a string "9E2" in a record without evaluation? Why web2py is trying to interpretate value in a string field?


Solution

  • If you are using SQLite, you may have initially defined the field as type double (or decimal) and later "migrated" to type string. As noted here, such migrations have no effect in SQLite, so SQLite will continue to treat the column as numeric and coerce values to numbers when possible (SQLite does not complain about storing non-numeric strings in the column because it does not have rigid column types -- it simply has type affinities, which determine what types of coercions may happen -- see https://www.sqlite.org/datatype3.html).

    You'll have to re-create the table, starting with the correct column type.