I am using SQlAlchemy automap extension for my preference to avoid manually writing up the declarative base classes. Mostly because I am not very familiar with the db schema and need to interact only with a small subset of tables and columns
I am using the following code to set up a SQlAlchemy session and then interact with my database. Things seem to work well using this.
engine = create_engine('mssql+pymssql://xx:xxx@xxx/xxx', implicit_returning=False)
metadata = MetaData()
tables = ['xxx', 'xxx', 'xxx']
metadata.reflect(engine, only=tables)
base = automap_base(metadata=metadata)
base.prepare()
session_maker = sessionmaker(bind=engine)
session = session_maker()
Since the database has a large number of tables and columns, it takes a fair amount of time for the session to load, since each time reflection has to be done.
Also, the IDE I use (PyCharm) gives me very useful auto completion when it can infer the object type correctly. It does not work when I use automap, for obvious reasons. It is often a challenge to get the column name strings right, and requires me to repeatedly verify them from db management studio.
Is there a way to generate declarative base classes using reflection? Can I at least persist the reflected metadata in some other way? It is safe to assume that db schema won't change.