Open In App

db.utils.NotSupportedError in Django

Last Updated : 11 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are fixing the “django.db.utils.NotSupportedError” problem that occurs in Django. First, let’s understand what is the problem. and how it occurs. and then we will look for the approaches to solve the problem.

What is ‘django.db.utils.NotSupportedError’

Syntax: django.db.utils.NotSupportedError: Database does not support unique_together on text fields.

(the description of the error may differ in your case).

This error occurs when you try to perform a database operation in Django and that is not supported by your database backend. For example, say you have a migration that tries to add a constraint:

Python3




from django.db import migrations
 
class Migration(migrations.Migration):
    dependencies = [
        ('store', '0001_initial'),
    ]
    operations = [
        migrations.AddField(
            model_name='product',
            name='price'
            field=models.DecimalField(max_digits=10, decimal_places=2),
        ),  
        migrations.AlterUniqueTogether(         <------------Here
            name='product'
            unique_together=set([('name', 'price')]


How to fix – django.db.utils.NotSupportedError?

To fix this error we have few options:

  • Check database settings.
  • Drop and recreate the database
  • Temporarily disable atomic transactions.

Check database Setting

First, ensure you are using a database backend. Django supports following databases officially:

Make sure the engine value in your DATABASES setting matches your database type.

For example: DATABASES = {

‘default’ : {

‘ENGINE’ :’django.db.backends.sqlite3′,

…}

}

If you are using a different database then you may need to install additional drivers to use it with Django. Install any additional drivers or dependencies required by your database, i.e. For PostgreSQL, install psycopg2. For MySQL, install MySQL client.

If you have already installed all the required packages or dependencies then try to check if the versions are supported. Try to update or downgrade the versions of drivers.

Drop and Recreate the Database

Make sure you carefully follow below steps. This step is not recommended in production otherwise you will loose all your data. Only use in your testing period.

  • Delete database file created by Django.

delete_db

  • Delete migrations folders created by Django in app folder.
    delete_migration_edit_3
  • Run “python manage.py makemigrations” command.
  • Run “python manage.py migrate” command.

migration&migrate

  • Enter Runserver command to run the application.

runserver

Above steps will make enable you to recreate the database.

Temporarily Disable Atomic Transactions

By default, Django transactions are atomic, which means all operations succeed or fail as a whole. but some databases do not support this so by setting atomic=false in your migration file can resolve this error in some cases.

Go to related migration file (After makemigrations command initial migrations file created automatically by django in migrations directory). Add atomic=false in the migration class.

e.g. from django.db import migrations, models

class Migration(Migrations.Migration):

atomic = False **** <–Here

initial = True

………………

Or you can remove the constraint from the migrations.

operations = [

migrations.AddField(

model_name=’product’,

name=’price’,

field=models.DecimalField(max_digits=10, decimal_places=2),

),

# Remove this line

# migrations.AlterUniqueTogether(

# name=’product’,

# unique_together=set([(‘name’, ‘price’)])

]

These are all the possible solutions that will help you to fix the “django.db.utils.NotSupportedError “.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads