title: Celery Flask Database Connection Error
body: <p>I want to access my postgresql database from my celery worker.</p>
  
  <p>Since I'm using factory pattern, I used <a href="https://github.com/mattupstate/overholt/blob/master/overholt/factory.py" rel="nofollow noreferrer">https://github.com/mattupstate/overholt/blob/master/overholt/factory.py</a> this link to create my celery worker.</p>
  
  <p>I'm using <code>celery -A xyz.jobs worker</code> command to start my celery worker.</p>
  
  <p><code>jobs.py</code> file:</p>
  
  <pre><code>from xyz.models.user import User
  from xyz.app import create_celery_app
  
  celery = create_celery_app()
  
  @celery.task()
  def do_some_stuff():
      print(User.get_all())
  </code></pre>
  
  <p>When I call this task from one of my endpoints, I get the following error:</p>
  
  <pre><code>[2018-04-09 10:21:39,530: ERROR/ForkPoolWorker-8] Task xyz.jobs.do_some_stuff[b3a71e41-2335-470f-89b8-f4844c6eba1c] raised unexpected: AttributeError("'NoneType' object has no attribute 'drivername'",)
  Traceback (most recent call last):
    File "/Users/user/Documents/celeryTest/xyz_backend/venv/lib/python3.6/site-packages/sqlalchemy/util/_collections.py", line 999, in __call__
      return self.registry[key]
  KeyError: 4320768832
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File "/Users/user/Documents/celeryTest/xyz_backend/venv/lib/python3.6/site-packages/celery/app/trace.py", line 374, in trace_task
      R = retval = fun(*args, **kwargs)
    File "/Users/user/Documents/celeryTest/xyz_backend/xyz/app.py", line 30, in __call__
      return TaskBase.__call__(self, *args, **kwargs)
    File "/Users/user/Documents/celeryTest/xyz_backend/venv/lib/python3.6/site-packages/celery/app/trace.py", line 629, in __protected_call__
      return self.run(*args, **kwargs)
    File "/Users/user/Documents/celeryTest/xyz_backend/xyz/jobs.py", line 10, in do_some_stuff
      print(User.get_all())
    File "/Users/user/Documents/celeryTest/xyz_backend/xyz/models/__init__.py", line 47, in get_all
      return db.session.query(cls).filter_by(**kwargs).all()
    File "/Users/user/Documents/celeryTest/xyz_backend/venv/lib/python3.6/site-packages/sqlalchemy/orm/scoping.py", line 153, in do
      return getattr(self.registry(), name)(*args, **kwargs)
    File "/Users/user/Documents/celeryTest/xyz_backend/venv/lib/python3.6/site-packages/sqlalchemy/util/_collections.py", line 1001, in __call__
      return self.registry.setdefault(key, self.createfunc())
    File "/Users/user/Documents/celeryTest/xyz_backend/venv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2950, in __call__
      return self.class_(**local_kw)
    File "/Users/user/Documents/celeryTest/xyz_backend/venv/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 143, in __init__
      bind = options.pop('bind', None) or db.engine
    File "/Users/user/Documents/celeryTest/xyz_backend/venv/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 877, in engine
      return self.get_engine()
    File "/Users/user/Documents/celeryTest/xyz_backend/venv/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 896, in get_engine
      return connector.get_engine()
    File "/Users/user/Documents/celeryTest/xyz_backend/venv/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 556, in get_engine
      self._sa.apply_driver_hacks(self._app, info, options)
    File "/Users/user/Documents/celeryTest/xyz_backend/venv/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 830, in apply_driver_hacks
      if info.drivername.startswith('mysql'):
  AttributeError: 'NoneType' object has no attribute 'drivername'
  </code></pre>
  
  <p>I'm assuming that, the problem is with the database connection, but could not solve it. Is there anything that I'm missing?</p>
  
  <p><code>app.py</code> file:</p>
  
  <pre><code>def create_celery_app(app=None):
      app = app or create_app()
      celery = Celery(__name__, broker=app.config['CELERY_BROKER_URL'])
      celery.conf.update(app.config)
      TaskBase = celery.Task
  
      class ContextTask(TaskBase):
          abstract = True
  
          def __call__(self, *args, **kwargs):
              with app.app_context():
                  return TaskBase.__call__(self, *args, **kwargs)
  
      celery.Task = ContextTask
      return celery
  
  
  def create_app():
      app = Flask(__name__)
      app.config.from_object(Config)
      register_extensions(app)
      register_api(app)
  
      return app
  
  
  def register_extensions(app):
      CORS(app)
      db.init_app(app)
      migrate.init_app(app, db)
  
  
  def register_api(app):
      from xyz.api.user import auth_api_blueprint
      app.register_blueprint(auth_api_blueprint, url_prefix='/auth')
  </code></pre>
  
  <p><code>extensions.py</code> file:</p>
  
  <pre><code>from flask_migrate import Migrate
  from flask_sqlalchemy import SQLAlchemy
  
  db = SQLAlchemy()
  migrate = Migrate()
  </code></pre>