Issue: Mysql gone away with peewee as second database in Django
Author: srugano
State: closed
Created: 2019-08-22 04:57:02

I'm using `peewee` to access a remote *MySql* database to retrieve data that I need to display in a *Django* app. It means that `peewee` isn't really used to access the main db but just to like, define custom models:

Example in `databases.py` :

    from django.conf import settings
    from playhouse.pool import PooledMySQLDatabase
    
    database = PooledMySQLDatabase(
        settings.DB_PEEWEE,
        **{
            "use_unicode": True,
            "charset": "utf8",
            "max_connections": 32,
            "stale_timeout": 300,  # 5 minutes.
            "password": settings.DB_PEEWEE_PSWRD,
            "user": settings.DB_PEEWEE_USER,
            "host": settings.DB_PEEWEE_HOST,
            "port": settings.DB_PEEWEE_PORT,
        }
    )

in `models.py`:


    from .databases import database
       
    
    class BaseModel(peewee.Model):
        class Meta:
            database = database 
    
    
    class CountryGroups(BaseModel):
        africa = peewee.CharField(null=True)
        group_code = peewee.AutoField()
        group_name = peewee.CharField()
        latin_eur = peewee.CharField(null=True)
        type = peewee.CharField()
    
        class Meta:
            table_name = "country_groups"
    ...
    # other main django models

So the model can be easily called from the `views.py` file as :

    CountryGroups_list = (
            CountryGroups.select()
            .where(CountryGroups.group_name << ["ERROR", "INFO"])
            .order_by(CountryGroups.group_name.desc())
            .limit(1000)
        )

I can run the query fine. But I get an error after 24 hours where the connection is broken:

`(2006, "MySQL server has gone away (error(32, 'Broken pipe'))")`

The suggested method of solving this in `Django` is trough the usage of a [middleware][1] but this assume that in that case `peewee` related db is the main one, and has resulted in errors like this one: 

    File "/home/user/Dev/project/project/wsgi.py", line 14, in <module>
        from configurations.wsgi import get_wsgi_application
      File "/home/user/.virtualenvs/project/lib/python3.5/site-packages/configurations/wsgi.py", line 14, in <module>
        application = get_wsgi_application()
      File "/home/user/.virtualenvs/project/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
        return WSGIHandler()
      File "/home/user/.virtualenvs/project/lib/python3.5/site-packages/django/core/handlers/wsgi.py", line 135, in __init__
        self.load_middleware()
      File "/home/user/.virtualenvs/project/lib/python3.5/site-packages/django/core/handlers/base.py", line 37, in load_middleware
        mw_instance = middleware(handler)
    TypeError: object() takes no parameters

So my question is, how would I implement the auto **connect()** and **close()** to my generic *database* model so that I don't get the  error?

  [1]: http://docs.peewee-orm.com/en/latest/peewee/database.html#django

> The suggested method of solving this in Django is trough the usage of a middleware but this assume that in that case peewee related db is the main one, and has resulted in errors like this one:

That has nothing to do with "solving" your connection timing out and being closed. That middleware is simply to ensure you are cleanly opening and releasing connections when Django views are accessed... as, presumably, you are accessing the database during the views.

The error you are receiving, "object() takes no parameters", is related to the API for middlewares changing in Django 2.x. https://docs.djangoproject.com/en/2.2/topics/http/middleware/

You'll need to implement this:

```python
class SimpleMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response
        # One-time configuration and initialization.

    def __call__(self, request):
        database.connect()

        response = self.get_response(request)

        if not database.is_closed(): database.close()
        return response
```

This is a fairly special usage of Peewee and you should be knowledgeable about both Django and Peewee... By your issue and the description, it seems to me that this is not the case for you. Probably you should stick with Django.