In an multi-seller platform type of application, where there is one database storing all of the transactions of multiple sellers, sequential receipt numbers must be issued by each seller for each sale for record keeping purposes without gaps in the receipt numbering sequence. What schema and approaches could one consider for this requirement?
My initial thought is to create a table with pre-populated sequential serial numbers (and optional prefixes as assigned by the seller) and a status column with values like 'available', 'pending', 'filled' as well as a 'sale_id' that will reference the 'sales' table:
table: serial_numbers
id | seller_id | sale_id | prefix | serial_number | status
1 1 NULL NULL 1 available
2 2 1 A 1000 filled
3 1 NULL NULL 2 available
4 2 NULL A 1001 pending
5 1 NULL A 1002 available
When a block of serial numbers gets low (say below 10 records available), I would insert 100 new serial number records in the database and set status to available. When a purchase is made, I would begin a new database transaction (which could be rolled back if the process is cancelled or terminated) which would set the next available serial number for the sellers account to pending and if the sale is completed, set the status to filled and relate the 'sale_id' to serial_number record.
Is this an efficient design? Using a simple auto increment is not possible because of the multi-seller constraint on the database. Are there other similar design pattern for multi-seller database applications?
Aucun commentaire:
Enregistrer un commentaire