3

I want to add a new table in existing database without affecting existing tables and its datas. After adding new model class into models.py, what are steps needs to follow to add new table?

asked Jun 6, 2020 at 20:59
2

1 Answer 1

1

Once you've added the new model in your models.py, you just need to run db.create_all(). You can add this in your code somewhere (probably directly) after you initialize your app/database. Be sure to include with app.app_context() if you aren't calling create_all in a view. Hope this helps!

answered Jun 7, 2020 at 3:51
2
  • 2
    Will using db.create_all() reset the data in the existing tables? Commented Mar 21, 2021 at 5:57
  • I think create_all() first checks for the existence of the tables in the model and will only create tables that don't digging through the docs I found this example: employees.create(engine, checkfirst=True) where checkfirst is True by default Commented Jan 28, 2023 at 15:38

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.