I believe, that between inserts/updates and selects you should do commit

On Friday, September 1, 2017 at 10:15:00 PM UTC+3, Dave S wrote:
>
>
>
> On Friday, September 1, 2017 at 10:03:48 AM UTC-7, Ben Lawrence wrote:
>>
>> Also, if i am inserting within a for loop, should I place the db.commit() 
>> within the for loop too?
>>
>> On Friday, September 1, 2017 at 9:59:11 AM UTC-7, Ben Lawrence wrote:
>>>
>>> Good point, but I put the commits after the function returns as there 
>>> are a number of inserts in a variety of tables. So is it preferred to do a 
>>> commit after every insert instead of grouping them together for one big 
>>> commit?
>>> TIA Ben
>>>
>>
>
> Part of the answer has to do with rolling back if there is an error.  If 
> you need ALL changes undone if any one change fails, then the commit comes 
> last.  If you need the first changes to survive even if the later changes 
> fail, then sprinkle commits in between.  Usually, the first choice is what 
> people make.
>
> /dps
>
>
>>> On Friday, September 1, 2017 at 12:35:01 AM UTC-7, Val K wrote:
>>>>
>>>> from the book:
>>>> Remember to call db.commit() at the end of every task if it involves 
>>>> inserts/updates to the database. web2py commits by default at the end of a 
>>>> successful action but the scheduler tasks are not actions.
>>>>
>>>> On Friday, September 1, 2017 at 8:30:00 AM UTC+3, Ben Lawrence wrote:
>>>>>
>>>>> Thanks guys for your time. No I do not know what a _before_insert hook 
>>>>> is, so I doubt that there is one here.
>>>>>
>>>>> here is
>>>>>
>>>>> ################################################################################
>>>>>
>>>>> def insert_company(valid_name, valid_email):
>>>>>     """ Inserts a company record if there is a new domain_tag
>>>>>     """
>>>>>     c_id = 0 # is the reference to the company record
>>>>>     # what is the company domain ?
>>>>>     email_tup = valid_email.split(u'@')
>>>>>     if len(email_tup) > 1:  # it is an email address
>>>>>         c_domain = email_tup[1]
>>>>>     elif len(email_tup) == 1:
>>>>>         c_domain = valid_email
>>>>>     else: # give up
>>>>>         c_domain = valid_name.upper()
>>>>>     # use this to find the company, 
>>>>>     c_rows = db(db.company.domain_tag == c_domain).select()
>>>>>     if len(c_rows)==0:  # add the company
>>>>>         c_id = db.company.update_or_insert(
>>>>>             name = get_company_name(c_domain),
>>>>>             e_mail = valid_email,
>>>>>             domain_tag = c_domain )
>>>>>     else:  # length MUST be one
>>>>>         c_id = c_rows.last().id
>>>>>     return c_id
>>>>>
>>>>> ################################################################################
>>>>>
>>>>> You know what,
>>>>> I am running all this on ubuntu 16 in an LXD container with software 
>>>>> raid on the metal. I am really starting to wonder if DAL and postgres and 
>>>>> lxd and software raid are somehow getting ahead of themselves in timing? 
>>>>> Perhaps there is no feedback or not enough feedback of "operation 
>>>>> completed"  somewhere?
>>>>>
>>>>>  
>>>>> On Wednesday, August 30, 2017 at 1:59:04 AM UTC-7, Val K wrote:
>>>>>>
>>>>>> As I see, it's a scheduler task -  show all code of insert_company
>>>>>> Is there any _before_insert hook?
>>>>>>
>>>>>> On Wednesday, August 30, 2017 at 1:22:57 AM UTC+3, Ben Lawrence wrote:
>>>>>>>
>>>>>>> 2.15.3-stable+timestamp.2017.08.07.07.32.04
>>>>>>> (Running on nginx/1.10.3, Python 2.7.12)
>>>>>>>
>>>>>>>
>>>>>>> Hi
>>>>>>> I hesitate to ask this because it will betray my stupidity
>>>>>>>
>>>>>>> I have this:
>>>>>>>
>>>>>>>     *c_rows = db(db.company.domain_tag == c_domain).select()*
>>>>>>>     if *len(c_rows)==0*:  # add the company
>>>>>>>         c_id = db.company.insert(
>>>>>>>             name = get_company_name(c_domain),
>>>>>>>             e_mail = valid_email,
>>>>>>>             *domain_tag = c_domain )*
>>>>>>>     else:  # length MUST be one
>>>>>>>         c_id = c_rows.last().id
>>>>>>>
>>>>>>> and this the error:
>>>>>>>
>>>>>>> File "applications/remail/models/scheduler.py", line 120, in 
>>>>>>> insert_company
>>>>>>>     domain_tag = c_domain )
>>>>>>>   File "/home/www-data/web2py/gluon/packages/dal/pydal/objects.py", 
>>>>>>> line 734, in insert
>>>>>>>     ret = self._db._adapter.insert(self, row.op_values())
>>>>>>>   File 
>>>>>>> "/home/www-data/web2py/gluon/packages/dal/pydal/adapters/base.py", line 
>>>>>>> 486, in insert
>>>>>>>     raise e
>>>>>>> IntegrityError: duplicate key value violates unique constraint 
>>>>>>> "company_domain_tag_key"
>>>>>>> DETAIL:  Key (domain_tag)=(platronics.com) already exists.
>>>>>>>
>>>>>>>
>>>>>>> Wha?! if domain_tag is already there would len(c_rows) > 0  and so 
>>>>>>> would not try to insert the record?
>>>>>>>
>>>>>>>
>>>>>>>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to