Adding records into table while running the db init Adding records into table while running the db init sqlite sqlite

Adding records into table while running the db init


Before running the flask application you should instantiate the FLASK_APP and FLASK_ENV.

First Create a function for adding data. assume that the function name is run like yours,

def run(self):    # Create a new Faker and tell it how to create User objects    faker = Faker(      cls=studentStatus,      init={        "status_id": 1,        "name": "Waiting on Admission"      }    )    # Create 5 users    for user in faker.create(5):      print("Adding user: %s" % user)      self.db.session.add(user)

Now you can call this function in the app running file. Assume that my file has named as app.py.

from application import appif __name__ == '__main__':     run()     app.run(debug=True)

Now set the FLASK_APP and FLASK_ENV like below.

export FLASK_APP=app.pyexport FLASK_ENV=development

After that, you can run the below commands.

flask db initflask db  migrateflask db upgrade

this will create all the tables and the data what you want.

For more details, you can get from the below link. This link is for a Github repository. There I have done adding some data when app initiating. Just read the readme.MD then you can get some more insight about setting up the FLASK_APP.

Flask Data Adding when DB instantiating


humm, seems like you put a underscore in Student_status, try to remove it. And other thing, try to write your class in CamelCase, is more "Pythonic" way to write a code, when you run it, the SQLalchemy will transform this class name to student_status table automatically