Re: Need your help on how to format a product code
Posted: Thu Jul 15, 2021 11:57 am
Hi,
- Having a "`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE" isn't just for fun - it's to make sure every record has a unique identifier. Not having such is calling for trouble.
Product codes and product names are prone to change at any time - if only because the user can do it. This results in a fiasco, the way you do it. Besides, TEXT indexes tend to be ways slower than INT indexes.
. - If you have a valid id & want to use it to create nice prodCodes, you don't fiddle in LC, you do it with a trigger:
Now you just insert a ProdName (thus creating a new record & activating the trigger), and have fine id & prodCodes. (Change the '5' to the desired number of digits)
Code: Select all
CREATE TRIGGER "add_prodcode" AFTER INSERT ON "product" BEGIN UPDATE product SET prodcode = ('P' || printf('%05d', id)); END
. - This way you can change both prodCode & prodName as you desire - you always use the id to refer to the product.
If the product is not needed anymore, you don't delete the record but set a 'deleted' field to true for it - so the records of old sales documents still point to a valid "deleted product".
. - If you want to find the AUTOIMCREMENT value that will be used next for table 't_table', just ask the db:
Code: Select all
SELECT (`seq` +1) AS NextSeq FROM `sqlite_sequence` WHERE (`name` = 't_table');