将Django数据库迁移到postgresql后出错

wrrgggsh  于 2022-12-18  发布在  PostgreSQL
关注(0)|答案(1)|浏览(236)

我最近在django中将数据库从sqlite3更改为postgresql,以便为生产做准备。在www.example.com中进行更改后settings.py,我尝试进行迁移,但遇到错误。在运行python manage.py migrate --run-syncdb后,我在终端中遇到以下错误

System check identified some issues:

WARNINGS:
users.Document.uploaded_at: (fields.W161) Fixed default value provided.
        HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`
users.Order.amount: (fields.W122) 'max_length' is ignored when used with IntegerField.
        HINT: Remove 'max_length' from field
users.buyer.uploaded_at: (fields.W161) Fixed default value provided.
        HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`
Operations to perform:
  Synchronize unmigrated apps: channels, chatapp, crispy_forms, messages, payments, staticfiles, users
  Apply all migrations: admin, auth, contenttypes, sessions
Synchronizing apps without migrations:
  Creating tables...
    Creating table users_user
    Creating table users_document
    Creating table users_buyer
    Creating table users_category
    Creating table users_service
    Creating table users_profile
    Creating table users_gig
    Creating table users_order
    Creating table users_product
    Running deferred SQL...
Traceback (most recent call last):
  File "/home/kiprono/anaconda3/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: relation "auth_group" does not exist

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

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/kiprono/anaconda3/lib/python3.7/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/home/kiprono/anaconda3/lib/python3.7/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/kiprono/anaconda3/lib/python3.7/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/kiprono/anaconda3/lib/python3.7/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/home/kiprono/anaconda3/lib/python3.7/site-packages/django/core/management/base.py", line 89, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/kiprono/anaconda3/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 214, in handle
    self.sync_apps(connection, executor.loader.unmigrated_apps)
  File "/home/kiprono/anaconda3/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 353, in sync_apps
    self.stdout.write('    Running deferred SQL...')
  File "/home/kiprono/anaconda3/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 118, in __exit__
    self.execute(sql)
  File "/home/kiprono/anaconda3/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 145, in execute
    cursor.execute(sql, params)
  File "/home/kiprono/anaconda3/lib/python3.7/site-packages/django/db/backends/utils.py", line 98, in execute
    return super().execute(sql, params)
  File "/home/kiprono/anaconda3/lib/python3.7/site-packages/django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/kiprono/anaconda3/lib/python3.7/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/kiprono/anaconda3/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/kiprono/anaconda3/lib/python3.7/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/kiprono/anaconda3/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "auth_group" does not exist

在我的www.example.com中models.py,我使用了AbstractUser模型,下面是我在www.example.com中的数据库settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'demo',
        'USER': 'demouser',
        'PASSWORD': '12345678',
        'HOST': 'localhost',
        'PORT': '',
    }
}

任何帮助都将不胜感激。谢谢。

juzqafwq

juzqafwq1#

错误消息“relation auth_group does not exist”表明Django正在尝试迁移引用auth_group表,但该表在您的数据库中不存在。这可能是因为授权应用的初始迁移尚未应用,或者是因为auth_group表已从数据库中删除。
要解决此问题,你可能需要对身份验证应用程序应用初始迁移。你可以通过运行以下命令来执行此操作:
python manage.py migrate auth
如果这不能解决问题,您可能需要删除任何挂起的迁移并重新创建它们。您可以通过运行以下命令来执行此操作:

python manage.py migrate auth zero
python manage.py makemigrations auth
python manage.py migrate auth

相关问题