I am also needing to find out max value of a field , seems like it is undocumented.
Is it exist? Where in api doc should i look for. On 4/4/11, Anthony <abasta...@gmail.com> wrote: > On Monday, April 4, 2011 9:39:31 AM UTC-4, Neveen Adel wrote: >> >> Thanks a lot Massimo :) >> >> The problem with this solution is that i have a table has member_id as >> foreign key and by this solution that data will be lost. > > > Are you saying in the legacy database, the 'member' table has a > 'membership_id' field, which is an auto-increment integer field and is > linked as a foreign key in one or more other tables? In that case, why > wouldn't Massimo's suggestion work? If you specify Field('membership_id', > type='id'), you are telling web2py not to create a field named 'id', but > instead to use the 'membership_id' field as the auto-incrementing primary > key for the 'member' table. I would think it would still work as a foreign > key in other tables in that case. Have you tried it? > > Also, as an aside, if you need the MAX of a field, I believe the web2py DAL > has a max() method (as well as min and sum), but it doesn't appear to be > documented in the book, and I'm not sure about the details of its usage. > Perhaps Massimo or someone else can elaborate (and maybe even update the > book). > > Anthony > > > >> On Apr 4, 3:31 pm, Massimo Di Pierro <massimo....@gmail.com> >> wrote: >> > how about >> > >> > db.define_table("member", >> > SQLField("membership_id", "id"), >> > SQLField("first_name", "string", notnull=True) >> > ) >> > >> > and "membership_id" would be your "id" field? >> > >> > On Apr 4, 8:03 am, Neveen Adel <nevo...@gmail.com> wrote: >> > >> > > Thanks Anthony for your reply. >> > >> > > The table already have an old data so i can't remove the column id or >> > > change in the database structure. >> > >> > > Is there another solution in controller level not database level? >> > >> > > Thanks in Advance >> > >> > > On Apr 4, 2:54 pm, Anthony <abas...@gmail.com> wrote: >> > >> > > > Your table already includes an 'id' field by default, which is an >> > > > auto-increment integer field starting at 1. Why do you need a >> separate >> > > > 'membership_id' field? If it's a legacy database and you need the >> name of >> > > > the 'id' field to be 'membership_id', you can simply define the >> > > > field >> type >> > > > as 'id' (seehttp:// >> web2py.com/book/default/chapter/06?search=auto-increment >> > > > ). >> > >> > > > Also, you should use Field() instead of SQLField() -- they're both >> the same, >> > > > but the latter has been deprecated in favor of the former. >> > >> > > > Anthony >> > >> > > > On Monday, April 4, 2011 8:35:00 AM UTC-4, Neveen Adel wrote: >> > > > > Hello, >> > >> > > > > I have the following table: >> > >> > > > > db.define_table("member", >> > > > > SQLField("membership_id", "integer",notnull=True), >> > > > > SQLField("first_name", "string", notnull=True) >> > > > > ) >> > >> > > > > and i want the membership id to be incremented automatically. >> > >> > > > > the way i used : >> > >> > > > > every time i inserted it , i select the max membership_id and >> adding >> > > > > one on its value as: >> > >> > > > > result=db.executesql("select max(membership_id) from member") >> > > > > record=result[0] >> > > > > if record[0]: >> > > > > next_membership_id = record[0]+1 >> > > > > else: >> > > > > next_membership_id=1 >> > > > > form.vars.membership_id=next_membership_id >> > >> > > > > But this solution allows duplicates?? >> > >> > > > > could anyone tell me what is the perfect solution? >> > >> > > > > Thanks in Advance > >