Hi all,
I’ve got a rails and database question. Accounts have jobs, and job
numbers should be assigned per account. Account 89 should have jobs
1-whatever, which are not the same as account 67, which has jobs
1-whatever.
In the past, I have accomplished this quite easily with:
create table job (
accountid int(10) unsigned not null default 0,
jobid int(10) unsigned not null auto_increment,
… other fields …
primary key (accountid, jobid)
);
Then, when inserting into the table, mysql takes care that the jobid is
the highest for the given account, and unique as well.
The rails way is to create one numeric key for the whole table, and then
use a specifically named column for the foreign key:
create table jobs (
id int(10) unsigned not null auto_increment
account_id int(10) unsigned not null default 0,
jobseq int(10) unsigned not null, # not assigned by mysql 
… other fields …
primary key (id)
);
Which is workable, but now the job numbers aren’t specific to the
customer, as I would like them to be. In fact, in this model, they are
not supplied or managed by the database at all.
Is there a safe way to generate the next jobseq during the insert
operation, such that the jobseq is the next job for the given customer,
and, most importantly, two jobs for the same customer cannot have the
same jobseq assigned, even accidentally?
Many thanks.
Regards,
Rich