在Django Mod中添加日期字段后迁移出错

2024-10-02 06:27:55 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的Django模型,在这个模型中,除了先前存在的字段之外,我还想添加另一个字段

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

当我运行'makemigrations myapp'时没有问题,但是当我运行'migrate'命令时,控制台显示一些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)

我认为主要问题在于:

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

但是我明确地说null=True。你知道吗

编辑 类迁移(迁移。迁移)地址:

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

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

Tags: runnameinpytrueexecutedatalib
1条回答
网友
1楼 · 发布于 2024-10-02 06:27:55

我通过删除一些迁移文件解决了这个问题。现在起作用了。我试着重新运行makemigration和migrate,但没有遇到任何问题。我认为依赖先例文件是问题所在

相关问题 更多 >

    热门问题