MariaDB driver for Django

Background Info/Context:

So I was reading through the Django Documentation, specifically this page that deals with Databases:

I noticed that under MariaDB notes, all that is written is:

  1. Django supports MariaDB > 10.4 version
  2. To use MySQL backend, which is used between the two
  3. To see MySQL notes

However under MySQL notes you see two options:

  • mysqlclient
  • MySQL Connector/Python

However the curious line to note is that:
“In addition to a DB API driver, Django needs an adapter to access the database drivers from its ORM”

&

  1. “Django provides an adapter for mysqlclient”
  2. “while MySQL Connector/Python includes its own”

Main Question

What I’m curious about is if instead of ‘MySQL Connector/Python’ … ‘MariaDB Connector/Python’ would work as well?

I know there is a strong interchangeability between MySQL and MariaDB, but in order to avoid the rare odd issue, since I am using MariaDB itself, I want to use MariaDB Connector/Python as well.

I’m testing it on my own as well, but I was hoping if anyone would share their own experience about this particular driver.

ALSO if the MariaDB Connector/Python is widely known to work (or NOT to work) with Django seamlessly, then I hope the documentation could be updated to reflect the same.

Found this discussion on issues tracker but its from 3 years ago: #32075 (New database backend for MariaDB Connector/Python) – Django

Edit 1:
After installing mysqlclient … I expected that

python -c "import MariaDB" would work but it doesn’t … however
python -c "import MySQLdb" works (case sensitive).

Which means that any reference inside the Application Python code would have to use the mysql schema instead of Mariadb

Edit 2: Also found this on MariaDB Jira …
https://jira.mariadb.org/browse/CONPY-121

Notice the comment by someon named Anel Husakovic

" At least it should be mentioned in Django documentation that MariaDB connector/Python can be used in conjunction with mysqlclient, not depending on default-libmysqlclient-dev but on libmariadb-dev."

I’m not sure I understand what you’re looking for here.

Quoting directly from the docs:

To use MariaDB, use the MySQL backend, …

I think this is about as direct as it can be.

Also, as you point out, ticket #32705 covers this situation. The status is “wontfix”, which means that right now, nothing is going to change as far as core Django is concerned.

The final post in that ticket says:

It sounds like a third-party database backend is the best way to proceed.

leaving open the possibility of someone putting together a third-party package to directly implement using the MariaDB client. That’s definitely an option for anyone interested enough in the specific feature differences that may exist between those two clients.

The problem Ken is Mariadb can do distinct recordset queries and mysql cannot. When using the mysqlclient with mariadb database backend in django and u try to perform a distinct query django, throws an error “django.db.utils.NotSupportedError: DISTINCT ON fields is not supported by this database backend.”
I use this feature all the time in other programming languages throughout my programming history. There are work arounds but they can be long and untenable sometimes.

That’s interesting to know, thanks!

Hi Ken,
I did some more research and contacted mariadb and the folks behind mariadb connector and it turns out that the reason DISTINCT on field tosses an error is bcuz awhile ago django blocked it from working and only allows DINTINCTS in Postgres . So I’m told. This to me seems wrong. So i figured out today a way to use DISTINCT with mysqlclient and mariadb backend.

e = TableName.objects.values(‘field_name1’).
distinct().order_by(‘field_name1’)

This works really well. Just like other databases i have used DISTINCT or unique query set.

Thank you for your time

As far as it goes, that’s a reasonably accurate statement. (To be more precise, the code is written in the other direction. The use of DISTINCT is not allowed unless the backend explicitly allows it. Right now, only PostgreSQL permits it.)

You could fork the MySQL backend, and change that parameter to see if it works as expected with MariaDB. I’m guessing it probably wouldn’t be that much work to create a custom engine class with that value overridden.