Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does django create a migration file when we change the storage attribute of FileField, when the storage type is not stored in the database?

I don't want to create a migration file whenever I change the storage of the FileField. I am getting the storage class from settings.py and it is configurable.

settings.py

Storage =  S3BotoStorage(bucket='example')

models.py

 from django.conf import settings

 class myModel(models.Model):
        file = models.FileField(upload_to='', blank=True, storage=settings.Storage)
like image 400
Vaibhav Singh Avatar asked Dec 14 '16 08:12

Vaibhav Singh


1 Answers

TLDR: It's an empty migration, it's harmless let it be reading any further or trying different things is probably just a waste of time

When ever you make a change to a model django has to make a migration because it needs to keep track of what changes have been made to a model over time. However that does not always mean that a modification will be made in the database. The migration produced here is an empty one. Your migration probably looks something like this and you will say, hay that's not empty!!

class Migration(migrations.Migration):

    dependencies = [
        ('stackoverflow', '0010_jsonmodel'),
    ]

    operations = [
        migrations.AlterField(
            model_name='jsonmodel',
            name='jfield',
            field=stackoverflow.models.MyJsonField(),
        ),
        migrations.AlterField(
            model_name='parent',
            name='picture',
            field=models.ImageField(storage=b'Bada', upload_to=b'/home/'),
        ),
    ]

But it is!! just do

./manage.py sqlmigrate <myapp> <migration_number>

And you will find that it does not produce any SQL! Quote from the manual as suggested by @sayse

Django will make migrations for any change to your models or fields - even options that don’t affect the database - as the only way it can reconstruct a field correctly is to have all the changes in the history, and you might need those options in some data migrations later on (for example, if you’ve set custom validators).

like image 177
e4c5 Avatar answered Nov 14 '22 23:11

e4c5