Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django.db.utils.NotSupportedError in sqlite why not supported in sqlite

Tags:

sqlite

django

class M_Post(models.Model):
''''
CODE
''''
class M_File(models.Model):
....
CODE
....

class M_Post_File(models.Model):
    post = models.ForeignKey(M_Post,on_delete=models.CASCADE)
    file = models.ForeignKey(M_File,on_delete=models.CASCADE,null=True)

error:

django.db.utils.NotSupportedError: Renaming the 'posts_file' table while in a transaction is not supported on SQLite because it would break referential integrity. Try adding atomic = False to the Migration class.

how to solve this error

like image 275
유재영 Avatar asked Jan 31 '18 18:01

유재영


5 Answers

Go to related migration file(automatically created in migrations directory after makemigrations command) and add atomic = False to the Migration class. Migration(migrations.Migration):. Then you can migrate the changes.

example code:

# Generated by Django 2.1.14 on 2019-12-02 07:07

from django.db import migrations, models


class Migration(migrations.Migration):
    atomic = False # **<<< HERE**

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='ebayLog',
            fields=[
like image 121
Selim Yılmaz Avatar answered Sep 22 '22 09:09

Selim Yılmaz


If you still have the problem here is a example:

# Generated by Django 2.1 on 2018-08-16 21:22

from django.db import migrations


class Migration(migrations.Migration):
    atomic = False # <<<< THIS LINE

    dependencies = [
        ('shop', '0004_product_imgfeat'),
    ]

    operations = [
        migrations.RenameModel(
            old_name='Category',
            new_name='CategoryShop',
        ),
    ]
like image 31
Rodrigo Avatar answered Sep 19 '22 09:09

Rodrigo


I migrated many times after I got this error.

Then I did what Selim said above and I also added atomic = False after class Migration(migrations.Migration): in every migration file, which was a little silly because I didn't know which file was THE related migration file...

Then I searched the "atomic=False" in Django documentation and I found these:1 2

As the error "Renaming the 'posts_file' table while in a transaction is not supported on SQLite" described we know that renaming while in a transaction is not supported on SQLite, so adding atomic=False is needed. But I don't know about DDL transactions so that's too much for me...

like image 25
veranopolestar Avatar answered Sep 21 '22 09:09

veranopolestar


If you don't want to touch anything and you've just started your project (well not necessarily), you can just delete the migration files inside the migrations directory and migrate again.

Otherwise, change the atomic variable in the migrations file to False then you can migrate your changes.

like image 21
Ismail Hachimi Avatar answered Sep 21 '22 09:09

Ismail Hachimi


Another way, if atomic=false method didn't work, is you can delete generated files in the migration folder and start again by make migrations and migrate

like image 35
Khyar Ali Avatar answered Sep 18 '22 09:09

Khyar Ali