[Maypole] Auto Increment Primary Keys (and Documentation)

Dave Howorth dhoworth@mrc-lmb.cam.ac.uk
Thu, 27 Jan 2005 10:57:44 +0000


Gordon Haverland wrote:
> I have a couple of tables which are related, one of which has an 
> auto_increment primary key.  I can create a tuple (row/object) of 
> data without ever mentioning this primary key as I understand 
> things.  Fine.  But, is there some easy way to find out just what 
> value was assigned to that variable?  For all the power of all 
> the modules behind Maypole, do I really need to search for the 
> maximum value in the primary key column after I create the tuple?  
> It seems to me, that if there is a high access rate on the 
> application, that this isn't going to get me the primary key 
> value I want anyway, some other process may have created another 
> tuple in that same table between when my process created the 
> tuple and when it searched to find the maximum value of the 
> primary key.
> 
> I would have thought this would be a common requirement, since the 
> value of the primary key column in a tuple is often used as a 
> foreign key in other tables.  But I just don't see it anywhere.  
> Are the creates for the associated tables supposed to get this 
> value automagically?

Does this answer your questions?

   my $brewery = Brewery->create({
	name => 'Anheuser Busch',
   })

   print $brewery->id;

   my $beer = Beer->create({
	name    => 'Budweiser',
	brewery => $brewery,
	style   => 'lager',
   });

   print $beer->id;

Cheers, Dave