从头开始创建sqlite时,无法创建django_migrations表(数据库已锁定)错误

时间:2018-05-03 22:09:05

标签: django python-3.x sqlite

我有一个与Django dev服务器一起运行的项目。所以我将它上传到GitHub,然后将其下载到我的预生产环境并尝试使用Apache2进行部署。但我一直得到数据库锁定错误。我不需要任何当前数据,我只想创建一个新的SQLite数据库。

删除数据库文件和所有迁移文件后,我认为我很高兴重新开始。但是当我运行manage.py migrate时,我得到以下错误。再次说数据库被锁定了。

Unable to create the django_migrations table (database is locked) 

我的settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db1.sqlite3'),
    }
}

完整的错误消息:

Operations to perform:
  Apply all migrations: account, admin, auth, contenttypes, people, sessions, sites, socialaccount
Running migrations:
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/base.py", line 239, in _commit
    return self.connection.commit()
sqlite3.OperationalError: database is locked

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

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/recorder.py", line 55, in ensure_schema
    editor.create_model(self.Migration)
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/sqlite3/schema.py", line 28, in __exit__
    super().__exit__(exc_type, exc_value, traceback)
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/schema.py", line 92, in __exit__
    self.atomic.__exit__(exc_type, exc_value, traceback)
  File "/usr/local/lib/python3.5/dist-packages/django/db/transaction.py", line 212, in __exit__
    connection.commit()
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/base.py", line 261, in commit
    self._commit()
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/base.py", line 239, in _commit
    return self.connection.commit()
  File "/usr/local/lib/python3.5/dist-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/base.py", line 239, in _commit
    return self.connection.commit()
django.db.utils.OperationalError: database is locked

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/commands/migrate.py", line 200, in handle
    fake_initial=fake_initial,
  File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/executor.py", line 91, in migrate
    self.recorder.ensure_schema()
  File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/recorder.py", line 57, in ensure_schema
    raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc)
django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table (database is locked)

问题:我如何锁定数据库,更重要的是如何创建新数据库?

由于

1 个答案:

答案 0 :(得分:0)

原因:

  

“数据库被锁定”错误¶

     

SQLite意味着是一个轻量级数据库,因此无法支持高级别的并发性。 OperationalError:数据库已锁定错误表示您的应用程序正在经历比sqlite在默认配置中可以处理的更多并发性。此错误意味着一个线程或进程对数据库连接具有独占锁定,而另一个线程超时则等待释放锁定。

     

Python的SQLite包装器有一个默认的超时值,它确定第二个线程在超时之前允许等待锁的时间长度并引发OperationalError:数据库被锁定错误。

     

如果您收到此错误,可以通过以下方式解决:

     

切换到另一个数据库后端。在某个时刻,SQLite对于真实世界的应用程序来说太“精简”了,而且这些并发错误表明你已经达到了这一点。

     

重写代码以减少并发性并确保数据库事务是短暂的。

     

通过设置超时数据库选项来增加默认超时值:

'OPTIONS': {
        # ...
        'timeout': 20,
        # ...
    }
  

这会让SQLite等待一段时间再抛出“数据库被锁定”错误;它不会真正解决它们。

解决方法:

  1. 删除db1.sqlite3文件。同时关闭SQLite浏览器(如果它为db1.sqlite3打开)。然后再次尝试migrate

  2. 迁移到其他数据库,因为SQLite3不适合生产。您将来需要这样做,所以现在就试试吧。 (首选:Mysql) Link to connect app with mysql