Tajinder Singh
Tajinder Singh

Reputation: 1531

Error in migrate after adding DateField in Django Model

This is my Django model, in which I want to add another field, in addition to pre-existent fields

class Offerta(models.Model):
    #old pre-existent fields
    ...
    #the field I want to add
    data_rifiuto = models.DateField(null=True, blank=True)

When I run 'makemigrations myapp' there are no problems, but when I run the 'migrate' command, console shows some errors:

    Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/lib/python3/dist-packages/django/db/backends/sqlite3/base.py", line 337, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: NOT NULL constraint failed: myapp_offerta.data_rifiuto

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/pycharm-2017.2.3/helpers/pycharm/django_manage.py", line 43, in <module>
    run_module(manage_file, None, '__main__', True)
  File "/usr/lib/python3.5/runpy.py", line 205, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "/usr/lib/python3.5/runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/santoryu/richiestaTesi/manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)

I think the main problem is here:

sqlite3.IntegrityError: NOT NULL constraint failed: myapp_offerta.data_rifiuto

But I have explicitly say null=True.

EDIT class Migration(migrations.Migration):

    dependencies = [
    ('myapp', '0037_remove_offerta_data_rifiuto'),
    ]

    operations = [
    migrations.AddField(
        model_name='offerta',
        name='data_rifiuto',
        field=models.DateField(blank=True, null=True),
    ),
    ]

Upvotes: 0

Views: 80

Answers (1)

Tajinder Singh
Tajinder Singh

Reputation: 1531

I've solved by deleting some migration files. Now it's working. I've tried to re-run makemigration and migrate and I've didn't get any trouble. I think dependencies from precedent file were the problem

Upvotes: 1

Related Questions