there’s something I’m struggling to understand with SQLAlchamy from it’s documentation and tutorials.
I see how to autoload classes from a DB table, and I see how to design a class and create from it (declaratively or using the mapper()) a table that is added to the DB.
My question is how does one write code that both creates the table (e.g. on first run) and then reuses it?
I don’t want to have to create the database with one tool or one piece of code and have separate code to use the database.
Thanks in advance,
Peter
create_all()does not do anything if a table exists already, so just call it as soon as you set up your engine or connection.(Note that if you change your table schema,
create_all()will not update it! So you still need “another program” to do that.)This is the usual pattern: