【问题标题】:Unable to create the django_migrations table (database is locked) error when creating sqlite from scratch从头开始创建 sqlite 时无法创建 django_migrations 表(数据库已锁定)错误
【发布时间】:2018-10-14 07:49:23
【问题描述】:

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

删除数据库文件和所有迁移文件后,我认为我可以重新开始。但是当我运行manage.py migrate 时,出现以下错误。再次说数据库已锁定。

Unable to create the django_migrations table (database is locked) 

我的设置.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)

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

谢谢

【问题讨论】:

    标签: django python-3.x sqlite


    【解决方案1】:

    原因:

    “数据库被锁定”错误¶

    SQLite 旨在成为一个轻量级数据库,因此无法支持高级别的并发。 OperationalError: database is locked 错误表明您的应用程序遇到的并发性超出了 sqlite 在默认配置中可以处理的数量。这个错误意味着一个线程或进程在数据库连接上有一个排他锁,另一个线程超时等待锁被释放。

    Python 的 SQLite 包装器有一个默认的超时值,它决定了第二个线程在超时之前允许在锁上等待多长时间并引发 OperationalError: database is locked 错误。

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

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

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

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

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

    这只会让 SQLite 在抛出“数据库已锁定”错误之前等待更长的时间;它不会真正解决它们。

    解决方法:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-10
      • 2020-03-22
      • 1970-01-01
      相关资源
      最近更新 更多