[Python] flask sqlalchemy column constraint for positive integer

Discussão em 'Python' iniciado por Stack, Agosto 11, 2025.

  1. Stack

    Stack Membro Participativo

    how can i define a column as a positive integer using flask sqlalchemy?

    i am hoping the answer would look something like this:

    class City(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    population = db.Column(db.Integer, positive=True)
    def __init__(self,population):
    self.population = population


    however, this class definition will throw an error b/c sqlalchemy does not know about a 'positive' argument.

    i could raise an exception if an object is instantiated with a negative value for the population. but i don't know how to ensure that the population remains positive after an update.

    thanks for any help.

    Continue reading...

Compartilhe esta Página