if home is None:
home_id = db.home.insert(user=id_user)
home = db.home(user=id_user)
else:
home_id = home.id
I google translator flip the code :S
El viernes 23 de marzo de 2012 13:31:13 UTC-4:30, www.diazluis.com escribió:
>
> sr. antonio has every reason
> after analyzing its response, the reasons are obvious DAL designed.
>
> the end the error of my logic (I apologize)
>
> missing return to the query of the data, after creating the record, I mean:
>
> if home is None:
> db.home.insert home_id = (user = id_user)
> db.home home = (user = id_user)
> else:
> home_id = home.id
>
> and the system returns in view as follows:
> return dict (home=home, form=form)
>
>
>
> El viernes 23 de marzo de 2012 12:59:50 UTC-4:30, Anthony escribió:
>>
>> In web2py, each request is wrapped in a single database transaction --
>> that way if an error occurs somewhere in the processing of the request, any
>> database changes can be rolled back automatically. It would not be
>> desirable to immediately commit each change (i.e., each insert, update,
>> delete, etc.) by default because when an error occurs during a request, you
>> could end up with only partial (and therefore inconsistent) changes. If you
>> need more fine-grained, control, though, you can call db.commit() and
>> db.rollback() as necessary.
>>
>> Anthony
>>
>> On Friday, March 23, 2012 12:58:22 PM UTC-4, www.diazluis.com wrote:
>>>
>>> Greetings sr.
>>> Thanks for your time.
>>>
>>> your explanation makes sense but ...
>>>
>>> but I find it annoying to have to be running explicitly "db.commit ()"
>>> something as simple as what you showed you
>>>
>>> Waste time having to revise the code to see where failure to use
>>> "db.commit ()" and so and keep it from bouncing an error like the one
>>> mentioned.
>>>
>>> DAL internally in each operation should execute "db.commit ()" or
>>> something (I have no technical knowledge to give a solution working)
>>>
>>>
>>> I justify the use of "db.commit ()"
>>> * If my app estubiera overloaded with queries to the database
>>> * Tubiera a background process working with the database
>>> * Operations on the database from a cycle "for"
>>>
>>> but NOT in the following steps:
>>> 1) check if a record exists
>>> 2) create the record if necessary
>>> 3) re-check the registry, to be used in crud.update
>>>
>>> I find that something is wrong
>>>
>>>
>>>
>>> El viernes 23 de marzo de 2012 09:24:41 UTC-4:30, Richard escribió:
>>>>
>>>> Most of the time in web2py you don't need to commit, but in particular
>>>> scenario you maybe need to commit before an other operation because you
>>>> need the database to be up to date before the and of your function.
>>>>
>>>>
>>>> http://web2py.com/books/default/chapter/29/6?search=db.commit%28%29<http://web2py.com/books/default/chapter/29/6?search=db.commit%28%29>
>>>>
>>>> Hope it helps
>>>>
>>>>
>>>> The following code gives me error if not explicitly use db.commit ()
>>>>> can someone explain why I now need to use db.commit () to something so
>>>>> simple?
>>>>>
>>>>> models/home.py
>>>>>
>>>>> ##############
>>>>> id_user = (auth.user and auth.user.id) or None
>>>>>
>>>>> db.define_table('home',
>>>>> Field('titulo', 'string', length=64, default='titulo de prueba'),
>>>>> Field('texto', 'text', length=5000, default='texto de prueba'),
>>>>> Field('user', db.auth_user, default=id_user, writable=False,
>>>>> readable=False),
>>>>>
>>>>> #auditoria
>>>>> Field('ip', 'string', length=64, default=request.client,
>>>>> update=request.client, writable=False, readable=False),
>>>>> Field('fecha', 'date',default=request.now, update=request.now,
>>>>> writable=False, readable=False),
>>>>> Field('user_update', db.auth_user, default=id_user,
>>>>> update=id_user, writable=False, readable=False),
>>>>> )
>>>>> ############
>>>>>
>>>>>
>>>>> controllers/default.py
>>>>> ##############
>>>>> def index():
>>>>>
>>>>> if auth.is_logged_in() :
>>>>> home = db.home(user=id_user)
>>>>> if home is None: redirect(URL('update'))
>>>>>
>>>>> return dict()
>>>>>
>>>>>
>>>>> @auth.requires_login()
>>>>> def update():
>>>>>
>>>>> home = db.home(user=id_user)
>>>>>
>>>>> if home is None:
>>>>> home_id = db.home.insert(user=id_user)
>>>>> else:
>>>>> home_id = home.id
>>>>>
>>>>> # db.commit()
>>>>> form = crud.update(db.home, home_id, deletable=False,
>>>>> next=URL('update'))
>>>>>
>>>>> return dict(home=home, form=form)
>>>>> #############
>>>>>
>>>>>
>>>>> view/default/update.html
>>>>>
>>>>> {{extend 'layout.html'}}
>>>>>
>>>>> <div>
>>>>> <span id="span_home_titulo">{{ =home.titulo }}</span>
>>>>> </div>
>>>>> <div id="form_home" >
>>>>> {{=form.custom.begin}}
>>>>> {{ =form.custom.widget.titulo }}
>>>>> {{ =form.custom.widget.texto }}
>>>>> <input type="submit" value="Save" />
>>>>> {{=form.custom.end}}
>>>>> </div>
>>>>>
>>>>> ###############
>>>>>
>>>>>
>>>>> Error
>>>>> #########
>>>>>
>>>>> web2py™ (1, 99, 7, datetime.datetime(2012, 3, 4, 22, 12, 8), 'stable')
>>>>> Python Python 2.6.6: /usr/bin/python
>>>>> TRACEBACK
>>>>>
>>>>> Traceback (most recent call last):
>>>>> File "/home/diazluis2007/web2py/gluon/restricted.py", line 205,
>>>>> in restricted
>>>>> exec ccode in environment
>>>>> File
>>>>> "/home/diazluis2007/web2py/applications/about/views/default/update.html",
>>>>> line 80, in <module>
>>>>> AttributeError: 'NoneType' object has no attribute 'titulo'
>>>>>
>>>>>
>>>>>
>>>>> Díaz Luis
>>>>> Analista Programador Facultad de Odontología UC
>>>>> http://www.about.me/diazluis
>>>>> User Linux 532223
>>>>>
>>>>
>>>>