Django and MySQL NDB Cluster

243 views
Skip to first unread message

Victor Guimarães Nunes

unread,
Jan 22, 2020, 3:19:42 PM1/22/20
to Django users
Hi,

I'm having some trouble setting up a Django project with a MySQL NDB Cluster. I found I had to set the storage engine on DATABASES['default']['OPTIONS'] and I did it.

DATABASES = {
'default': config('DATABASE_URL', cast=db_url)
}

# Check if mysql database engine is NDBCLUSTER
if config('USE_NDBCLUSTER', cast=bool, default=False):
DATABASES['default'].update({
'OPTIONS': {
'init_command': 'SET default_storage_engine=NDBCLUSTER;',
}
})

but almost always when I try to perform some write action on the database I get the following esception:

OperationalError at /admin/auth/user/add/

(1178, "The storage engine for the table doesn't support SAVEPOINT")

I don't quite get what I supposed to do about once the cluster demands a NDBCLUSTER storage engine to work properly and I don't have any ideia of what is savepoints and where/how I can disabled it.

Any thoughts about this issue?

Jason

unread,
Jan 22, 2020, 4:32:04 PM1/22/20
to Django users
https://code.djangoproject.com/ticket/27677

Check the last comment from Tim.

maninder singh Kumar

unread,
Jan 23, 2020, 2:49:16 AM1/23/20
to django...@googlegroups.com
Could it be the server doesn't have the database ?

 
               
 


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d3b6be1a-acb1-4543-b8f2-780b8ef5b62d%40googlegroups.com.

Victor Guimarães Nunes

unread,
Jan 23, 2020, 6:33:06 AM1/23/20
to Django users
Will do

Victor Guimarães Nunes

unread,
Jan 23, 2020, 6:33:44 AM1/23/20
to Django users
No, the database is all set and includes some data by the way.


Em quinta-feira, 23 de janeiro de 2020 04:49:16 UTC-3, maninder singh Kumar escreveu:
Could it be the server doesn't have the database ?

 
               
 


On Thu, Jan 23, 2020 at 1:49 AM Victor Guimarães Nunes <nuness...@gmail.com> wrote:
Hi,

I'm having some trouble setting up a Django project with a MySQL NDB Cluster. I found I had to set the storage engine on DATABASES['default']['OPTIONS'] and I did it.

DATABASES = {
'default': config('DATABASE_URL', cast=db_url)
}

# Check if mysql database engine is NDBCLUSTER
if config('USE_NDBCLUSTER', cast=bool, default=False):
DATABASES['default'].update({
'OPTIONS': {
'init_command': 'SET default_storage_engine=NDBCLUSTER;',
}
})

but almost always when I try to perform some write action on the database I get the following esception:

OperationalError at /admin/auth/user/add/

(1178, "The storage engine for the table doesn't support SAVEPOINT")

I don't quite get what I supposed to do about once the cluster demands a NDBCLUSTER storage engine to work properly and I don't have any ideia of what is savepoints and where/how I can disabled it.

Any thoughts about this issue?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Victor Guimarães Nunes

unread,
Jan 23, 2020, 6:42:27 AM1/23/20
to Django users
I found a (dummy) solution for this. The error was occurring when a Signal from post_save of User model was being triggered. In that signal I was performing a SomeModel.objects.get_or_create.

So, apparently the get_or_create method was implemented using transactions with savepoints enabled. All I had to do was check if the Signal is called when the instance of SomeModel is created and use SomeMode.objects.create instead of SomeMode.objects.get_or_create.

But it still sucks tho, because everytime I use @transaction.atomic I have to pass a savepoint=False parameter to it. The sucky part is, if I'm undertanding it right, I have to implement my own database backend only to disable savepoints by default on all transactions and I don't thinks it worths the effort.

Well, anyway, if someone already knows where I can find a MySQL NDBCLUSTER implementation of a django database backend I would be glad. Thank you all!

Victor Guimarães Nunes

unread,
Jan 24, 2020, 6:41:31 AM1/24/20
to Django users
Yesterday I figured how to solve this issue using a custom Database backend. I'm posting this here to help anyone have the same problem.

1. Create a new python package for it (I recommend use poetry for it):

django-mysql-nosavepoint/
├── django_mysql_nosavepoint
│   ├── base.py
│   └── __init__.py
├── LICENSE
├── pyproject.toml
└── README.rst

2. Use poetry to add the dependencies:

poetry add django mysqlclient

3. Add the following code to base.py:

from django.db.backends.mysql import base


class DatabaseFeatures(base.DatabaseFeatures):
uses_savepoints = False


class DatabaseWrapper(base.DatabaseWrapper):
features_class = DatabaseFeatures


4. Use poetry to build the package:

poetry build

5. Install the package with pip:

pip install django_mysql_nosavepoint-0.1.0-py3-none-any.whl

6. Use your new backend of settings.py

DATABASES = {
'default': {
# other stuff
'ENGINE': 'django_mysql_nosavepoint',
}
}

That's it. Hope it helps someone
Em quarta-feira, 22 de janeiro de 2020 17:19:42 UTC-3, Victor Guimarães Nunes escreveu:
Reply all
Reply to author
Forward
0 new messages