# Buggy code
def get_or_create_main_db(caravel):
    db = caravel.db
    config = caravel.app.config
    DB = caravel.models.Database
    logging.info("Creating database reference")
    dbobj = db.session.query(DB).filter_by(database_name="main").first()
    if not dbobj:
        dbobj = DB(database_name="main")
    logging.info(config.get("SQLALCHEMY_DATABASE_URI"))
    dbobj.sqlalchemy_uri = config.get("SQLALCHEMY_DATABASE_URI")
    dbobj.expose_in_sqllab = True
    db.session.add(dbobj)
    db.session.commit()
    return dbobj


# Traceback (OperationalError)
2016-09-07 01:52:51,509:ERROR:root:(pymysql.err.OperationalError) (1045, u"Access denied for user 'mysqladmin'@'172.18.0.3' (using password: NO)")
Traceback (most recent call last):
File "/home/caravel/caravel/caravel/views.py", line 66, in wraps
return f(self, *args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/Flask_AppBuilder-1.8.1-py2.7.egg/flask_appbuilder/security/decorators.py", line 52, in wraps
return f(self, *args, **kwargs)
File "/home/caravel/caravel/caravel/views.py", line 1118, in tables
'tables': database.all_table_names(schema),
File "/home/caravel/caravel/caravel/models.py", line 445, in all_table_names
return sorted(self.inspector.get_table_names(schema))
File "/home/caravel/caravel/caravel/models.py", line 442, in inspector
return sqla.inspect(engine)
File "build/bdist.linux-x86_64/egg/sqlalchemy/inspection.py", line 63, in inspect
ret = reg(subject)
File "build/bdist.linux-x86_64/egg/sqlalchemy/engine/reflection.py", line 139, in _insp
return Inspector.from_engine(bind)
File "build/bdist.linux-x86_64/egg/sqlalchemy/engine/reflection.py", line 135, in from_engine
return Inspector(bind)
File "build/bdist.linux-x86_64/egg/sqlalchemy/engine/reflection.py", line 109, in __init__
bind.connect().close()
File "build/bdist.linux-x86_64/egg/sqlalchemy/engine/base.py", line 2018, in connect
return self._connection_cls(self, **kwargs)
File "build/bdist.linux-x86_64/egg/sqlalchemy/engine/base.py", line 72, in __init__
if connection is not None else engine.raw_connection()
File "build/bdist.linux-x86_64/egg/sqlalchemy/engine/base.py", line 2104, in raw_connection
self.pool.unique_connection, _connection)
File "build/bdist.linux-x86_64/egg/sqlalchemy/engine/base.py", line 2078, in _wrap_pool_connect
e, dialect, self)
File "build/bdist.linux-x86_64/egg/sqlalchemy/engine/base.py", line 1405, in _handle_dbapi_exception_noconnection
exc_info
File "build/bdist.linux-x86_64/egg/sqlalchemy/util/compat.py", line 202, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "build/bdist.linux-x86_64/egg/sqlalchemy/engine/base.py", line 2074, in _wrap_pool_connect
return fn()
File "build/bdist.linux-x86_64/egg/sqlalchemy/pool.py", line 318, in unique_connection
return _ConnectionFairy._checkout(self)
File "build/bdist.linux-x86_64/egg/sqlalchemy/pool.py", line 713, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "build/bdist.linux-x86_64/egg/sqlalchemy/pool.py", line 480, in checkout
rec = pool._do_get()
File "build/bdist.linux-x86_64/egg/sqlalchemy/pool.py", line 1060, in _do_get
self._dec_overflow()
File "build/bdist.linux-x86_64/egg/sqlalchemy/util/langhelpers.py", line 60, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "build/bdist.linux-x86_64/egg/sqlalchemy/pool.py", line 1057, in _do_get
return self._create_connection()
File "build/bdist.linux-x86_64/egg/sqlalchemy/pool.py", line 323, in _create_connection
return _ConnectionRecord(self)
File "build/bdist.linux-x86_64/egg/sqlalchemy/pool.py", line 449, in __init__
self.connection = self.__connect()
File "build/bdist.linux-x86_64/egg/sqlalchemy/pool.py", line 607, in __connect
connection = self.__pool._invoke_creator(self)
File "build/bdist.linux-x86_64/egg/sqlalchemy/engine/strategies.py", line 97, in connect
return dialect.connect(*cargs, **cparams)
File "build/bdist.linux-x86_64/egg/sqlalchemy/engine/default.py", line 385, in connect
return self.dbapi.connect(*cargs, **cparams)
File "/usr/local/lib/python2.7/site-packages/pymysql/__init__.py", line 90, in Connect
return Connection(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/pymysql/connections.py", line 690, in __init__
self.connect()
File "/usr/local/lib/python2.7/site-packages/pymysql/connections.py", line 908, in connect
self._request_authentication()
File "/usr/local/lib/python2.7/site-packages/pymysql/connections.py", line 1116, in _request_authentication
auth_packet = self._read_packet()
File "/usr/local/lib/python2.7/site-packages/pymysql/connections.py", line 983, in _read_packet
packet.check_error()
File "/usr/local/lib/python2.7/site-packages/pymysql/connections.py", line 395, in check_error
err.raise_mysql_exception(self._data)
File "/usr/local/lib/python2.7/site-packages/pymysql/err.py", line 107, in raise_mysql_exception
raise errorclass(errno, errval)
OperationalError: (pymysql.err.OperationalError) (1045, u"Access denied for user 'mysqladmin'@'172.18.0.3' (using password: NO)")

# Fixed code
def get_or_create_main_db(caravel):
    db = caravel.db
    config = caravel.app.config
    DB = caravel.models.Database
    logging.info("Creating database reference")
    dbobj = db.session.query(DB).filter_by(database_name="main").first()
    if not dbobj:
        dbobj = DB(database_name="main")
    logging.info(config.get("SQLALCHEMY_DATABASE_URI"))
    dbobj.set_sqlalchemy_uri(config.get("SQLALCHEMY_DATABASE_URI"))
    dbobj.expose_in_sqllab = True
    db.session.add(dbobj)
    db.session.commit()
    return dbobj