Autoincrement field

1,167 views
Skip to first unread message

ceriox

unread,
Jan 15, 2010, 5:41:33 AM1/15/10
to web2py-users
hi all,
i need an autoincremented unic numeric field for make a ticket system
how i can do it?

thanks for help

Alexandre Andrade

unread,
Jan 15, 2010, 8:14:41 AM1/15/10
to web...@googlegroups.com
by default, web2py creates a 'id' field, it is autoincremented.



2010/1/15 ceriox <cer...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To post to this group, send email to web...@googlegroups.com.
To unsubscribe from this group, send email to web2py+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/web2py?hl=en.






--
Atenciosamente

--
=========================
Alexandre Andrade
Hipercenter.com

Thadeus Burgess

unread,
Jan 15, 2010, 11:32:42 AM1/15/10
to web...@googlegroups.com
Field('mysecondaryid', 'integer', default=db.table.mysecondaryid + 1)

should work.

-Thadeus

mdipierro

unread,
Jan 15, 2010, 11:46:09 AM1/15/10
to web2py-users
No it will not.

On Jan 15, 10:32 am, Thadeus Burgess <thade...@thadeusb.com> wrote:
> Field('mysecondaryid', 'integer', default=db.table.mysecondaryid + 1)
>
> should work.
>
> -Thadeus
>

> On Fri, Jan 15, 2010 at 7:14 AM, Alexandre Andrade <alexandrema...@gmail.com


>
> > wrote:
> > by default, web2py creates a 'id' field, it is autoincremented.
>
> > 2010/1/15 ceriox <cer...@gmail.com>
>
> >> hi all,
> >> i need an autoincremented unic numeric field for make a ticket system
> >> how i can do it?
>
> >> thanks for help
>
> >> --
>
> >> You received this message because you are subscribed to the Google Groups
> >> "web2py-users" group.
> >> To post to this group, send email to web...@googlegroups.com.
> >> To unsubscribe from this group, send email to

> >> web2py+un...@googlegroups.com<web2py%2Bunsu...@googlegroups.com>


> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/group/web2py?hl=en.
>
> > --
> > Atenciosamente
>
> > --
> > =========================
> > Alexandre Andrade
> > Hipercenter.com
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "web2py-users" group.
> > To post to this group, send email to web...@googlegroups.com.
> > To unsubscribe from this group, send email to

> > web2py+un...@googlegroups.com<web2py%2Bunsu...@googlegroups.com>

Thadeus Burgess

unread,
Jan 15, 2010, 11:54:17 AM1/15/10
to web...@googlegroups.com

ceriox

unread,
Jan 19, 2010, 5:32:11 AM1/19/10
to web2py-users
thanks for the reply but i'm not a good web2py programmer ... i
writing my first real app
you can write the code for my request? (i can't understand the post of
your link)

Thadeus Burgess

unread,
Jan 19, 2010, 2:38:46 PM1/19/10
to web...@googlegroups.com
max_id= db(db.table.autonumber>1).select(db.table.autonumber, #
select all records, and only pull the autonumber column
orderby=~db.table.autonumber, #
descending sort on the autonumber, (highest first)
limitby=(0,1) # limit the query and
only select the first record
).first().autonumber # pull the first record
from the web2py rows object, and get its autonumber member

db.table.autonumber.default = max_id + 1 # Set the table default as
the last autonumber and incremented by one.
db.table.autonumber.writable = False

form = crud.create(db.table)

-Thadeus

matclab

unread,
May 27, 2010, 4:16:47 PM5/27/10
to Thadeus Burgess, web...@googlegroups.com
Hello,
I'm finding this message in a thread from February...

I thought that autoincremented field would guaranty unicity in the
table.
I'm afraid the provided solution would allow two record to have the
same autonumber field (think about an access from two users at the
same time).
I guess the autoincrement should be done on the DAL or database side,
inside a transaction....

What do you think about it ?

mdipierro

unread,
May 27, 2010, 5:25:31 PM5/27/10
to web2py-users
What is wrong with the default id field that web2py creates for every
table?

Iceberg

unread,
May 28, 2010, 3:10:00 AM5/28/10
to web2py-users
I guess the point here is, generally speaking, what is a secure way to
make two or more subsequent DAL operations are performed without race
condition. Is a kind of lock needed? An example to describe the
problem (but not solving it):

def index():
with a_lock_to_prevent_concurrent_requests: # but how?
latest_data = db(...).select(...)
new_data = do_something_with( latest_data )
db.mytable.insert( new_data )

See also: http://groups.google.com/group/web2py/browse_frm/thread/35be6bba0c02eab0#

Mathieu Clabaut

unread,
May 28, 2010, 4:44:34 AM5/28/10
to web...@googlegroups.com
I guess that one may want some more constraints on the autoincrement field. For example, it shall begin at 100  for ecample.
It may be because for example, before the application exists some paper records where made which were referenced by number < 100)

For such a problem, I've set up with:

Field('ref_number', compute=lambda r: r['id'] + 100)

Which I hope would eliminate the race solution as it is calculate upon insertion in the database... Is it the case ?

-Mathieu

mdipierro

unread,
May 28, 2010, 10:02:23 AM5/28/10
to web2py-users
yes

On May 28, 3:44 am, Mathieu Clabaut <mathieu.clab...@gmail.com> wrote:
> I guess that one may want some more constraints on the autoincrement field.
> For example, it shall begin at 100  for ecample.
> It may be because for example, before the application exists some paper
> records where made which were referenced by number < 100)
>
> For such a problem, I've set up with:
>
> Field('ref_number', compute=lambda r: r['id'] + 100)
>
> Which I hope would eliminate the race solution as it is calculate upon
> insertion in the database... Is it the case ?
>
> -Mathieu
>
> > web2py+un...@googlegroups.com<web2py%2Bunsu...@googlegroups.com>
> > .

szimszon

unread,
Jan 28, 2013, 9:44:22 AM1/28/13
to web...@googlegroups.com
Hi!

I wonder if somebody could help me.

The

definet_table
('sometable',
   
Field('otherid', compute=lambda r: r['id'])
)


doesn't work for me. :( Sould it work? With other than r['id'] it's working.

Version 2.4.1-alpha.2+timestamp.2013.01.27.10.24.17

Alec Taylor

unread,
Jan 28, 2013, 9:48:24 AM1/28/13
to web...@googlegroups.com
Which DB are you using BTW?

Because if you're using something like MySQL; they have inbuilt
autoincrement fields which would be much better to utilise than what
we provide in the DAL.

Maybe we should have an 'autoincrement' field, like we have a 'datetime' field?

Gracefully degrade to a lambda if there isn't native autoincrement support.
> --
>
>
>

szimszon

unread,
Jan 28, 2013, 9:55:42 AM1/28/13
to web...@googlegroups.com
postgresql Version: 8.4.12-0squeeze1 (producttion - not tested)
postgresql Version: 9.1.7-0ubuntu12.10 (dev)

I need somethink like:

Field('otherid','string', compute=lambda r: 'SOMETHING%s%s' % (
    str
(datetime.date.today()),
    str
(r['id'])))

>> > > web2py+un...@googlegroups.com<web2py%2Bunsubscribe@googlegroups.com>

szimszon

unread,
Jan 28, 2013, 9:56:55 AM1/28/13
to web...@googlegroups.com
I'm definitively for an auto-increment field like datetime anyway :-D


2013. január 28., hétfő 15:48:24 UTC+1 időpontban Alec Taylor a következőt írta:
>> > > web2py+un...@googlegroups.com<web2py%2Bunsubscribe@googlegroups.com>

szimszon

unread,
Jan 29, 2013, 3:13:29 AM1/29/13
to web...@googlegroups.com
Something about this issue? Should it work?
> > web2py+un...@googlegroups.com<web2py%2Bunsubscribe@googlegroups.com>

szimszon

unread,
Jan 29, 2013, 3:17:38 AM1/29/13
to web...@googlegroups.com
The really ugly thing is that compute can silently fail and you could end up with None in the db table cell :(

szimszon

unread,
Jan 29, 2013, 4:05:17 AM1/29/13
to web...@googlegroups.com
Some other issue:

db:
import datetime
db
.define_table('sometable',
   
Field('otherid',
          compute
=lambda r: 'SPPRIME%s' % (
                                    datetime
.date.today().strftime('%y%m%d'))
)




If I insert a db row from modules

current.db.sometable.insert()


The new record has otherid==None :(

Massimo Di Pierro

unread,
Jan 29, 2013, 9:15:59 AM1/29/13
to web...@googlegroups.com
Please open a ticket about this.

szimszon

unread,
Jan 29, 2013, 10:38:08 AM1/29/13
to web...@googlegroups.com

Alec Taylor

unread,
Jan 30, 2013, 7:11:19 AM1/30/13
to web...@googlegroups.com
On Wed, Jan 30, 2013 at 1:15 AM, Massimo Di Pierro
<massimo....@gmail.com> wrote:
> Please open a ticket about this.

http://code.google.com/p/web2py/issues/detail?id=1311

Alec Taylor

unread,
Feb 7, 2013, 7:16:42 AM2/7/13
to web...@googlegroups.com
"Optionally you can define a field of `type='id'` and web2py will use
this field as auto-increment id field. This is not recommended except
when accessing legacy database tables. With some limitation, you can
also use different primary keys and this is discussed in the section
on "Legacy databases and keyed tables"."
http://web2py.com/books/default/chapter/29/06#DAL,-Table,-Field
Reply all
Reply to author
Forward
0 new messages