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?
-
1checkout this link. You may find it helpful.Hasnain Ali– Hasnain Ali2020-06-06 21:09:55 +00:00Commented Jun 6, 2020 at 21:09
-
Does this answer your question? SQLAlchemy Add A Table To An Already Existing DatabasePavoDive– PavoDive2023-04-07 11:58:55 +00:00Commented Apr 7, 2023 at 11:58
Add a comment
|
1 Answer
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!
2 Comments
Constantine Westerink
Will using
db.create_all() reset the data in the existing tables?Peej1226
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