0

This is my Model:

class Category(db.Model):
    __tablename__='category'
    id = db.Column(db.Integer,primary_key=True)
    items = db.relationship('Item',backref='category',lazy='dynamic')
    name = db.Column(db.String(80))
    order = db.Column(db.Integer)
    private = db.Column(db.Boolean)
    color = db.Column(db.String(80),unique=False)

    def __init__(self,name,order=None,private=None):
        r = lambda: random.randint(0, 255)
        color = (r(), r(), r())
        color = ('#%02X%02X%02X' % color)
        count = db.session.query(Category).count()
        print count
        self.name = name
        self.color = color
        self.order = count+1
        self.private = 1

    def __repr__(self):
        return '<Category %r>' % self.name

and I create the tables here:

def initialize_tables():
    db.create_all()
    c = Category(name="uncategorized")
    db.session.add(c)
    db.session.commit()

if __name__ == '__main__':
    initialize_tables()
    app.run(debug=True)

This creates two categories in my database named 'uncategorized'. Why is this happening?

BigBoy1337
  • 4,735
  • 16
  • 70
  • 138

1 Answers1

1

That's because you're using app.run with debug=True:

If the debug flag is set the server will automatically reload for code changes and show a debugger in case an exception happened.

The way the reloader works is by starting itself again in a subprocess, so if __name__ == '__main__' (and hence initialize_tables) runs twice.

univerio
  • 19,548
  • 3
  • 66
  • 68