The problem with any solution that inspects the current records in the 
database table in order to figure out the ID for a new record is that you 
will not be able to account for deleted records (i.e., you might end up 
re-using IDs that were previously used by records that were later deleted). 
This approach also requires an extra query for every insert and has a 
potential race condition (unless you select for update).

Instead, you would probably need to store the starting record ID of the 
first record each month externally (e.g., in a separate db table). To avoid 
having to query that extra table before each insert, you could use the 
web2py cache to cache the current month's value and update it with a query 
only once a month.

Anthony

On Sunday, March 5, 2017 at 6:23:27 PM UTC-5, 黄祥 wrote:
>
> perhaps you can use count 
> *e.g.*
> count_invoice = db.invoice.id.count()
> this_year = request.now.year
> this_month = request.now.month
>
> query_invoice = ((db.invoice.invoice_date.year() == this_year) & 
> (db.invoice.invoice_date.month() == this_month) )
> query_count_invoice = 
> db(query_invoice).select(count_invoice).first()[count_invoice] if 
> db(query_invoice).select() else 0
>
> invoice_no_count = int(query_count_invoice) + 1 if query_count_invoice 
> else 1
> invoice_no_format_count = format(invoice_no_count, '05')
>
> invoice_no = request.now.strftime('%y%m')+invoice_no_format_count
>
> best regards,
> stifan
>

-- 
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