Answer the question
In order to leave comments, you need to log in
What exceptions should be caught in SQLAlchemy?
def setup_connection():
engine = create_engine("postgresql://[email protected]:5432/testdb", echo=False)
Session = sessionmaker(bind=engine)
sess = Session()
try:
Base.metadata.create_all(engine)
except Exception as e:
print("Could not initialize DB: {0}".format(e))
return sess
session = setup_connection()
Answer the question
In order to leave comments, you need to log in
Never catch Exception. This leads to the appearance of hidden errors and unstable states, and also leads to heavy debugging. As a last resort, it is worth catching SQLAlchemyError. But in general, the more specific the error handler, the better.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question