As noted in the documentation 
<http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#Legacy-databases-and-keyed-tables>,
 
the .update_record method is not available on rows of keyed tables, so you 
will therefore also not be able to use the built-in .update_or_insert 
method (which uses .update_record internally). Instead, you can write your 
own function -- as you can see here 
<https://github.com/web2py/pydal/blob/v16.06.28/pydal/objects.py#L792>, 
it's a fairly simple function. Also, feel free to submit a Github issue 
about this (link back to this discussion), as I don't think it would be 
difficult to refactor .update_or_insert to accommodate keyed tables.

Anthony

On Monday, July 11, 2016 at 2:11:38 AM UTC-4, Ívar Ragnarsson wrote:
>
> Hi
> I'm new to Web2py and having some trouble with update_or_insert.  Hope you 
> guys can help me.
> The first time I run the update_or_insert function it correctly inserts 
> the values into the table. (I'm using sqlite but also tried Postgresql).
> The next time I run it I get an Attribute:
> Traceback (most recent call last):
>   File "C:\...\gluon\restricted.py", line 227, in restricted
>     exec ccode in environment
>   File "C:/.../applications/pant/controllers/test.py", line 6, in <module>
>   File "C:\...\gluon\globals.py", line 417, in <lambda>
>     self._caller = lambda f: f()
>   File "C:/.../applications/pant/controllers/test.py", line 4, in index
>     db.stillingar.update_or_insert( (db.stillingar.notandi==1)&(db.
> stillingar.stilling=='setting1'), notandi=1 , gildi="111", stilling=
> "setting1")
>   File "C:\...\gluon\packages\dal\pydal\objects.py", line 786, in 
> update_or_insert
>     record.update_record(**values)
>   File "C:\...\gluon\packages\dal\pydal\objects.py", line 90, in 
> __getattr__
>     raise AttributeError
> AttributeError
>
> My table definition is:
> db.define_table('stillingar', 
>                 Field('notandi', type="integer", required=True), 
>                 Field('stilling', type="text", required=True),
>                 Field('gildi', type="text"),
>                 primarykey=['notandi', 'stilling'],
>                 migrate=True)
>
> My controller is:
> # -*- coding: utf-8 -*-
>
> def index():
>      db.stillingar.update_or_insert( (db.stillingar.notandi==1)&(db.
> stillingar.stilling=='setting1'), notandi=1 , gildi="111", stilling=
> "setting1")
>      return dict()
>
> My view is basic.
>
> Can you guys tell me what I'm doing wrong?
>
>
>
>

-- 
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 web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to