> To illustrate in Field() you
> have a length argument that puts a length constraint at the database level i.e
> the actual field definition (CHAR(10)). At the form level you also have
> IS_LENGTH which is used by SQLFORM to perform basically the same function.

Ah, but just as you wanted to do something that nobody else in the
web2py community has needed yet (I'm sure more will over time), it may
be that someone else will need something you haven't thought of -- in
this particular case, the disparity between your idea of what field
length definitions should be used for vs the behavior of most (all?)
SQL RDBMS.  In everything I've used, it is not an error (or even
necessarily a bad thing) to pass a 200 character string to a CHAR(50)
field, since the string will simply be truncated to the length of 50.

So you could have a field defined as:

CREATE TABLE message (
  id INT PRIMARY KEY,
  short_description VARCHAR(50) UNIQUE NOT NULL,
  content TEXT NOT NULL
)

This would allow you to do simple inserts like the following and have
them work as expected (optimize when optimization is needed):

db.message.insert(short_description=text, content=text)

>
> Traceback (most recent call last):
>   File "/home/aklaver/software_projects/w2py/web2py/gluon/restricted.py", line
> 188, in restricted
>     exec ccode in environment
>
> File 
> "/home/aklaver/software_projects/w2py/web2py/applications/hplc/controllers/appadmin.py",
> line 410, in <module>
>   File "/home/aklaver/software_projects/w2py/web2py/gluon/globals.py", line 
> 96,
> in <lambda>
>     self._caller = lambda f: f()
>
> File 
> "/home/aklaver/software_projects/w2py/web2py/applications/hplc/controllers/appadmin.py",
> line 124, in insert
>     form = SQLFORM(db[table], ignore_rw=ignore_rw)
>   File "/home/aklaver/software_projects/w2py/web2py/gluon/sqlhtml.py", line 
> 694,
> in __init__
>     default = field.formatter(default)
>   File "/home/aklaver/software_projects/w2py/web2py/gluon/sql.py", line 2869, 
> in
> formatter
>     value = item.formatter(value)
>   File "/home/aklaver/software_projects/w2py/web2py/gluon/validators.py", line
> 2023, in formatter
>     year = value.year
> AttributeError: 'Expression' object has no attribute 'year'
>
>
>
> > So perhaps this should go in the FAQ.
>
> --
> Adrian Klaver
> [email protected]

Reply via email to