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
-
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
1 Answer 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!
-
2Will using
db.create_all()
reset the data in the existing tables?Constantine Westerink– Constantine Westerink2021年03月21日 05:57:21 +00:00Commented 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 defaultPeej1226– Peej12262023年01月28日 15:38:29 +00:00Commented Jan 28, 2023 at 15:38
lang-py