I am currently developing an API for invoices. And I want to create an Invoice table with invoiceNumber field that would be auto incrementing but as a string. Something like this:
- INV00001
- INV00002
- INV00003
Is it possible to do it somehow using SQLAlchemy?
I tried to use the Sequence function from SQLAlchemy, but I know it is designed only for integers.
Using the SQL in this answer from Gordon Linoff, we can define a column with a default that combines an integer sequence and a string prefix like this:
The column definition depends on the existence of the sequence
test_seq. We can optionally add a pair of listeners to drop and create this sequence when the table is dropped or created to keep them in sync.