[Python] How do I create a both nullable and autoincremented column in SQLAlchemy?

Discussão em 'Python' iniciado por Stack, Fevereiro 17, 2025.

  1. Stack

    Stack Membro Participativo

    I have a SQLAlchemy model like this:

    class User(db.Model):
    id: Mapped[int] = mapped_column(primary_key=True)
    column: Mapped[int] = mapped_column(nullable=True, unique=True)


    How can I make it so column column can be null, and will be an autoincremented integer value otherwise?

    user1 = User() # here the `column` should be null
    user2 = User() # missing code to make `column` autoincremented value, so for example if the previous user had it as NULL here it would be the first value, 0


    I am using PostgreSQL and Flask-SQLAlchemy if this helps.

    Continue reading...

Compartilhe esta Página