KC64ML / gd_backend

0 stars 0 forks source link

소셜 로그인을 구현하기 위해 admin에 접근해야하는데 createsuperuser 계정 추가가 안됩니다ㅠㅠ #4

Closed KC64ML closed 3 years ago

KC64ML commented 3 years ago

createsuperuser 계정 추가에서 오류

기존 createsuperuser를 만들었는데 기억이 나지 않습니다.

kakao, naver 계정을 추가해보고 싶어 createsuperuser이 필요합니다.

1) 새로운 superuser를 만들려고 시도하였습니다.

KChang@LAPTOP-H1595VAD MINGW64 /d/Computer_Science/School/GraduationWork/gd_backend (master)
$ heroku run python manage.py createsuperuser
 »   Warning: heroku update available from 7.53.0 to 7.59.0.
Running python manage.py createsuperuser on ⬢ dongagd... up, run.4935 (Free)
Email: oka268@naver.com
Password: 
Password (again): 
Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", 
line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "gdapp_user_username_key"
DETAIL:  Key (username)=() already exists.

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

Traceback (most recent call last):
  File "/app/manage.py", line 22, in <module>
    main()
  File "/app/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 79, in execute
    return super().execute(*args, **options)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 189, in handle
    self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)  File "/app/gdapp/models.py", line 95, in create_superuser
    return self.create_user(email, password, **extra_fields)
  File "/app/gdapp/models.py", line 84, in create_user
    return self._create_user(email, password, **extra_fields)
  File "/app/gdapp/models.py", line 78, in _create_user
    user.save(using=self._db)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/auth/base_user.py", line 67, in save
    super().save(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/base.py", line 726, in save
    self.save_base(using=using, force_insert=force_insert,
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/base.py", line 763, in save_base
    updated = self._save_table(
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/base.py", line 868, in _save_table
    results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/base.py", line 906, in _do_insert
    return manager._insert(
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/manager.py", 
line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/query.py", line 1270, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1416, in execute_sql
    cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", 
line 98, in execute
    return super().execute(sql, params)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", 
line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", 
line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", 
line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/utils.py", line 90, 
in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", 
line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "gdapp_user_username_key"
DETAIL:  Key (username)=() already exists.

(gdvenv) 

IntegrityError가 발생합니다.

2) shell 편집기로 들어가, 비밀번호를 변경하려고 하였습니다.

KChang@LAPTOP-H1595VAD MINGW64 /d/Computer_Science/School/GraduationWork/gd_backend (master)
$ heroku run python manage.py shell
 »   Warning: heroku update available from 7.53.0 to 7.59.0.
Running python manage.py shell on ⬢ dongagd... up, run.5995 (Free)
Python 3.9.7 (default, Aug 30 2021, 22:04:11) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.contrib.auth.models import User
>>> User.objects.get(username="lkc263@naver.com", is_superuser=True).delete()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/manager.py", 
line 187, in __get__
    raise AttributeError(
AttributeError: Manager isn't available; 'auth.User' has been swapped for 'gdapp.User'>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit
>>>
now exiting InteractiveConsole...
(gdvenv) 

AttibuteError, 매니저가 이용할 수 없습니다. auth.User를 gdapp.User로 바뀌었습니다. 라는 것을 보고

settings에 auth.User를 추가해야겠구나 라는 생각에 auth.User를 추가하였습니다.

Inkedsettingspy에 추가하니_LI

$ git push heroku master # 까지는 잘되지만,
$ heroku run python manage.py migrate # 오류
KChang@LAPTOP-H1595VAD MINGW64 /d/Computer_Science/School/GraduationWork/gd_backend (master)
$ heroku run python manage.py migrate
 »   Warning: heroku update available from 7.53.0 to 7.59.0.
Running python manage.py migrate on ⬢ dongagd... up, run.5025 (Free)
Traceback (most recent call last):
  File "/app/manage.py", line 22, in <module>
    main()
  File "/app/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 363, in execute
    settings.INSTALLED_APPS
  File "/app/.heroku/python/lib/python3.9/site-packages/django/conf/__init__.py", line 82, in __getattr__
    self._setup(name)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/conf/__init__.py", line 69, in _setup
    self._wrapped = Settings(settings_module)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/conf/__init__.py", line 170, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/app/.heroku/python/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/app/gdproject/settings.py", line 18, in <module>
    User = get_user_model()
  File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/auth/__init__.py", line 160, in get_user_model
    return django_apps.get_model(settings.AUTH_USER_MODEL, require_ready=False)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/apps/registry.py", line 201, in get_model
    self.check_apps_ready()
  File "/app/.heroku/python/lib/python3.9/site-packages/django/apps/registry.py", line 136, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
(gdvenv)

와 같은 오류가 발생합니다.

발생해서 구글링을 해보니

settings에 django.setup()을 추가하니

setup추가

KChang@LAPTOP-H1595VAD MINGW64 /d/Computer_Science/School/GraduationWork/gd_backend (master)
$ heroku run python manage.py migrate
 »   Warning: heroku update available from 7.53.0 to 7.59.0.
Running python manage.py migrate on ⬢ dongagd... up, run.2957 (Free)
Traceback (most recent call last):
  File "/app/manage.py", line 22, in <module>
    main()
  File "/app/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 89, in wrapped
    res = handle_func(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 92, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/migrations/executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/migrations/loader.py", line 53, in __init__
    self.build_graph()
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/migrations/loader.py", line 220, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
    if self.has_table():
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 55, in has_table
    with self.connection.cursor() as cursor:
  File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 259, in cursor
    return self._cursor()
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/dummy/base.py", line 20, in complain
    raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
(gdvenv) 

이러한 오류가 발생합니다ㅠㅠ

오늘 하루종일 찾고 있지만 해결하지 못하고 있어 적게 되었습니다ㅠㅠ

aciddust commented 3 years ago

음 이전 커밋내역 확인해서 돌려놓고 다시 해볼까요? Heroku에 배포된 서버와 그 서버가 바라보는 DB에 슈퍼유저가 생성되어야하네요?

python manage.py createsuperuser --settings=gdproject.settings_heroku 입력하면 슈퍼유저 생성 가능해요.

KC64ML commented 3 years ago
KChang@LAPTOP-H1595VAD MINGW64 /d/Computer_Science/School/GraduationWork/gd_backend (master)
$ python manage.py createsuperuser --settings=gdproject.settings_heroku
Email: lkc263@naver.com
Password: 
Password (again): 
Traceback (most recent call last):
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\django\db\backends\mysql\base.py", line 73, in execute
    return self.cursor.execute(query, args)
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\pymysql\cursors.py", line 148, in execute
    result = self._query(query)
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\pymysql\cursors.py", line 310, in _query
    conn.query(q)
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\pymysql\connections.py", line 548, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\pymysql\connections.py", line 775, in _read_query_result
    result.read()
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\pymysql\connections.py", line 1156, in read
    first_packet = self.connection._read_packet()
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\pymysql\connections.py", line 725, in _read_packet
    packet.raise_for_error()
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\pymysql\protocol.py", line 221, in raise_for_error
    err.raise_mysql_exception(self._data)
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\pymysql\err.py", line 143, in raise_mysql_exception
    raise errorclass(errno, errval)
pymysql.err.IntegrityError: (1062, "Duplicate entry '' for key 'username'")

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

Traceback (most recent call last):
  File "D:\Computer_Science\School\GraduationWork\gd_backend\manage.py", line 22, in <module>
    main()
  File "D:\Computer_Science\School\GraduationWork\gd_backend\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\django\core\management\__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 79, in execute
    return super().execute(*args, **options)
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\django\core\management\base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 189, in handle
    self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
  File "D:\Computer_Science\School\GraduationWork\gd_backend\gdapp\models.py", line 95, in create_superuser
    return self.create_user(email, password, **extra_fields)
  File "D:\Computer_Science\School\GraduationWork\gd_backend\gdapp\models.py", line 84, in create_user
    return self._create_user(email, password, **extra_fields)
  File "D:\Computer_Science\School\GraduationWork\gd_backend\gdapp\models.py", line 78, in _create_user
    user.save(using=self._db)
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\django\contrib\auth\base_user.py", line 67, in save
    super().save(*args, **kwargs)
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\django\db\models\base.py", line 726, in save
    self.save_base(using=using, force_insert=force_insert,
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\django\db\models\base.py", line 763, in save_base
    updated = self._save_table(
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\django\db\models\base.py", line 868, in _save_table
    results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\django\db\models\base.py", line 906, in _do_insert
    return manager._insert(
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\django\db\models\query.py", line 1270, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\django\db\models\sql\compiler.py", line 1416, in execute_sql
    cursor.execute(sql, params)
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\django\db\backends\utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\django\db\backends\mysql\base.py", line 73, in execute
    return self.cursor.execute(query, args)
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\pymysql\cursors.py", line 148, in execute
    result = self._query(query)
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\pymysql\cursors.py", line 310, in _query
    conn.query(q)
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\pymysql\connections.py", line 548, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\pymysql\connections.py", line 775, in _read_query_result
    result.read()
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\pymysql\connections.py", line 1156, in read
    first_packet = self.connection._read_packet()
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\pymysql\connections.py", line 725, in _read_packet
    packet.raise_for_error()
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\pymysql\protocol.py", line 221, in raise_for_error
    err.raise_mysql_exception(self._data)
  File "D:\Computer_Science\School\GraduationWork\gd2venv\lib\site-packages\pymysql\err.py", line 143, in raise_mysql_exception
    raise errorclass(errno, errval)
django.db.utils.IntegrityError: (1062, "Duplicate entry '' for key 'username'")
(gd2venv)

테스하기위해, 말씀하신 --settings=gdproject.settings_heroku 입력시 django.db.utils.IntegrityError가 발생합니다.

음... superuser가 이미 존재하여 발생하는걸까요??