Hi cjwebb
Your suspect is correct and I had a similar barrier. Pay attention
this part of log:
> File "c:\web2py\gluon\sqlhtml.py", line 1205, in accepts
> self.vars.id = self.table.insert(**fields)
Before the line 1205 "accepts" method tries to set up "recipe_id" with
"fields[fieldname]=".
Probaly you must pass request.vars as a first parameter to "accepts"
in your controller, and
"request.vars.recipe_id" is an empty str type.
Follow this sequence in the algorithm to understand what happens:
Line(1020)
fields = {}
....
Line(1155)
elif fieldname in self.vars:
fields[fieldname] = self.vars[fieldname]
....
Line (1160)
value = fields.get(fieldname,None)
**If you debug in this point, fields may return something like: {..,
'recipe_id': '',...} **
...
Line (1170)
elif field.type.startswith('reference'):
(1) --> if value != None and isinstance(self.table, Table) and not
keyed:
fields[fieldname] = safe_int(value)
This field is a "reference recipes" type and in "fields" variable is
empty intead None, So,
in (1) the decision is evaluated as True, and after "safe_int" is
called and returns "0".
In "my particular case", I have a table with a foreign key that
accepts null values
db.define_table(
'receivables',
Field(
'matrix_id',
db.matrices,
ondelete="SET NULL",
requires=[],
label=T('matrix_id')
),
Field(....
"requires=[]" overrides default validation, but I want keep the
feature "references" and
to use in queries. "SET NULL" assures that in database level I don't
have problems
when I erase a primary key in matrices.
To fix the problem of empty value in matrix_id, I check in my
controller
if "request.vars.matrix_id" is differente than None and if it is a
digit,
otherwise I set to None.
Also I can use autocomplete to store primary key from matrices
(In models)
db.receivables.matrix_id.widget=SQLFORM.widgets.autocomplete(
request,
db.matrices.matrix_composed,
id_field=db.matrices.id,
limitby=(0,10),
min_length=2,
)
Now I ask for everyone:
If the column is defined as "integer" and as a reference to
another table
in the models, its value is empty when the field is submited,
Web2py can
solve this situation by setting a null value or fetch the value
from
"default" parameter?
How can I avoid writing this kind of solution? Does anyone have
any suggestions?