Django 不会创建 SQLite3 数据库

问题描述 投票:0回答:1

所以我有一个与 MySQL 数据库一起使用的 django 项目。现在我正在按照 MDN 教程尝试将其部署在 Railway 上。我已将数据库从 MySQL 更改为 SQLite3。现在,当我尝试在本地计算机上运行它时,我收到此错误:

(venv) ~/Documents/FDM/Readinghood POD Project/readinghood $ python3 manage.py makemigrations readinghoodWARNING:root:No DATABASE_URL environment variable set, and so no databases setup
/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/venv/lib/python3.11/site-packages/airflow/configuration.py:241: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
  if StrictVersion(sqlite3.sqlite_version) < StrictVersion(min_sqlite_version):
Traceback (most recent call last):
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/venv/lib/python3.11/site-packages/django/db/backends/sqlite3/base.py", line 328, in execute
    return super().execute(query, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sqlite3.OperationalError: no such table: book

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

Traceback (most recent call last):
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/manage.py", line 22, in <module>
    main()
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/venv/lib/python3.11/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/venv/lib/python3.11/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/venv/lib/python3.11/site-packages/django/core/management/base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/venv/lib/python3.11/site-packages/django/core/management/base.py", line 453, in execute
    self.check()
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/venv/lib/python3.11/site-packages/django/core/management/base.py", line 485, in check
    all_issues = checks.run_checks(
                 ^^^^^^^^^^^^^^^^^^
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/venv/lib/python3.11/site-packages/django/core/checks/registry.py", line 88, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/venv/lib/python3.11/site-packages/django/core/checks/urls.py", line 42, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/venv/lib/python3.11/site-packages/django/core/checks/urls.py", line 61, in _load_all_namespaces
    url_patterns = getattr(resolver, "url_patterns", [])
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/venv/lib/python3.11/site-packages/django/utils/functional.py", line 57, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
                                         ^^^^^^^^^^^^^^^^^^^
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/venv/lib/python3.11/site-packages/django/urls/resolvers.py", line 715, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
                       ^^^^^^^^^^^^^^^^^^^
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/venv/lib/python3.11/site-packages/django/utils/functional.py", line 57, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
                                         ^^^^^^^^^^^^^^^^^^^
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/venv/lib/python3.11/site-packages/django/urls/resolvers.py", line 708, in urlconf_module
    return import_module(self.urlconf_name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/readinghood/urls.py", line 9, in <module>
    path('', include('readinghoodapp.urls')),
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/venv/lib/python3.11/site-packages/django/urls/conf.py", line 38, in include
    urlconf_module = import_module(urlconf_module)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/readinghoodapp/urls.py", line 2, in <module>
    from . import views
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/readinghoodapp/views.py", line 3, in <module>
    from .forms import SignUpForm, CheckoutForm, PaymentForm, post_form_factory, EditProfileForm, ProfilePicForm, BookFilterForm
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/readinghoodapp/forms.py", line 6, in <module>
    class BookFilterForm(forms.Form):
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/readinghoodapp/forms.py", line 7, in BookFilterForm
    category = forms.MultipleChoiceField(choices=[(category, category) for category in Book.objects.values_list(
                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/venv/lib/python3.11/site-packages/django/db/models/query.py", line 398, in __iter__
    self._fetch_all()
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/venv/lib/python3.11/site-packages/django/db/models/query.py", line 1881, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/venv/lib/python3.11/site-packages/django/db/models/query.py", line 285, in __iter__
    for row in compiler.results_iter(
               ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/venv/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1513, in results_iter
    results = self.execute_sql(
              ^^^^^^^^^^^^^^^^^
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/venv/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1562, in execute_sql
    cursor.execute(sql, params)
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 102, in execute
    return super().execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
    return executor(sql, params, many, context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 84, in _execute
    with self.db.wrap_database_errors:
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/venv/lib/python3.11/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/lionel/Documents/FDM/Readinghood POD Project/readinghood/venv/lib/python3.11/site-packages/django/db/backends/sqlite3/base.py", line 328, in execute
    return super().execute(query, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.OperationalError: no such table: book

我已经尝试了一切:

  • python manage.py makemigrations your_app_name
  • 删除db.sqlite3文件
  • python manage.py runserver --noreload
  • 卸载 Django

更改设置文件中的数据库代码:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'readinghood',
        'USER': 'root',
        'PASSWORD': ...,
        'HOST': 'localhost',
        'PORT': '3306',
    }
}

至:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}
django database sqlite
1个回答
0
投票

首先你可以尝试使用 python manage.py migrate ,这个命令在你的新数据库中创建所有表

© www.soinside.com 2019 - 2024. All rights reserved.