Skip to content Skip to sidebar Skip to footer

Auto-incrementing Attribute With Custom Logic In Sqlalchemy

I have a simple 'Invoices' class with a 'Number' attribute that has to be assigned by the application when the user saves an invoice. There are some constraints: 1) the application

Solution 1:

Is there any particular reason you don't just use a default= parameter in your column definition? (This can be an arbitrary Python callable).

def generate_invoice_number():
    # special logic to generate a unique invoice number

class Invoice(DeclarativeBase):
    __tablename__ = 'invoice'
    number = Column(Integer, unique=True, default=generate_invoice_number)
    ...

Post a Comment for "Auto-incrementing Attribute With Custom Logic In Sqlalchemy"