質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

0回答

2051閲覧

Djangoでのユーザー作成について

kazzzstudio

総合スコア94

Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2017/08/29 14:55

編集2017/08/29 23:41

お世話になっております。

Djangoを使用して、

python manage.py createsuperuser

コマンドを実行したところ、

django.db.utils.IntegrityError: duplicate key value violates unique constraint "authtoken_token_user_id_key"
DETAIL: Key (user_id)=(abcdef@gmail.com) already exists.

とエラーが返ってきてしまい、解決することができずにいます。

このコマンドを打つ前に、一度データベースをDROPして、migrationsディレクトリも
000* ファイルは全て消去した上で、migrateしていますので、該当すると思われる
authtoken_tokenテーブルも空の状態であり、何が悪くてこのエラーが発生しているのか
見当もつきません(Userテーブルも空です)。

どなたかご見識の方がいらっしゃいましたら、ぜひご教示いただけると幸いです。
よろしくお願いいたします。

追記(1)

dbshell実行時の出力は下記のようになります。

(remoshin) ubuntu@ip-172-31-33-33:~/remoshin/remoshin$ python3 manage.py makemigrations (remoshin) ubuntu@ip-172-31-33-33:~/remoshin/remoshin$ python3 manage.py migrate (remoshin) ubuntu@ip-172-31-33-33:~/remoshin/remoshin$ python3 manage.py dbshell psql (9.5.8) SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off) Type "help" for help. remoshin_maindb=# \dt List of relations Schema | Name | Type | Owner --------+-----------------------------------------------+-------+--------- public | auth_group | table | remosys public | auth_group_permissions | table | remosys public | auth_permission | table | remosys public | authtoken_token | table | remosys public | django_admin_log | table | remosys public | django_content_type | table | remosys public | django_migrations | table | remosys public | django_session | table | remosys public | remosys_chat_detail_tbl | table | remosys public | remosys_chat_tbl | table | remosys public | remosys_clinic_ipaddress_tbl | table | remosys public | remosys_clinic_open_tbl | table | remosys public | remosys_clinic_tbl | table | remosys public | remosys_consultation_menu_tbl | table | remosys public | remosys_consultation_tbl | table | remosys public | remosys_department_tbl | table | remosys public | remosys_error_tbl | table | remosys public | remosys_menu_tbl | table | remosys public | remosys_refund_request_tbl | table | remosys public | remosys_remoshin_doctor_tbl | table | remosys public | remosys_remoshin_manager_tbl | table | remosys public | remosys_remoshin_manager_tbl_groups | table | remosys public | remosys_remoshin_manager_tbl_user_permissions | table | remosys public | remosys_remoshin_user_tbl | table | remosys public | remosys_statement_tbl | table | remosys (25 rows) remoshin_maindb=# select * from authtoken_token; key | created | user_id -----+---------+--------- (0 rows) remoshin_maindb=# select * from remosys_remoshin_user_tbl; remoshin_maindb=# select * from authtoken_token_user_id_key"; remoshin_maindb"# select max(id) from authtoken_token_user_id_key; remoshin_maindb"# remoshin_maindb=# \q (remoshin) ubuntu@ip-172-31-33-33:~/remoshin/remoshin$ python3 manage.py createsuperuser (省略) File "/home/ubuntu/remoshin/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute return self.cursor.execute(sql, params) psycopg2.IntegrityError: duplicate key value violates unique constraint "authtoken_token_user_id_key" DETAIL: Key (user_id)=(abcdef@gmail.com) already exists.

追記2)
使用ソフトウェアのバージョン

python 3.5.2
OS Ubuntu 16.4 LTS (AWS)
Django-1.11.4

settings.py

import os from django.core.urlresolvers import reverse_lazy # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) DJANGO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) PROJECT_ROOT = os.path.dirname(DJANGO_ROOT) LOGIN_REDIRECT_URL = reverse_lazy('main') LOGIN_URL = reverse_lazy('login') LOGOUT_URL = reverse_lazy('logout') LOGIN_ERROR_URL = reverse_lazy('login') SECRET_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' DEBUG = True ALLOWED_HOSTS = ['app.myserver.com'] AUTH_USER_MODEL = 'user.RemoshinUser' AUTHENTICATION_BACKENDS = {'user.backends.RemoshinUserAuth',} # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', 'rest_framework.authtoken', 'corsheaders', 'remoshin', 'user', ] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] ROOT_URLCONF = 'remoshin.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates/registration')] WSGI_APPLICATION = 'remoshin_user.wsgi.application' # Database # https://docs.djangoproject.com/en/1.11/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'db_name', 'USER': 'db_user', 'PASSWORD' : 'db_password', 'HOST' : '127.0.0.1', 'PORT' : 5432, } } # Password validation # https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators AUTH_PASSWORD_VALIDATORS = [ { 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', }, { 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', }, { 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', }, { 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', }, ] # REST API用の設定 JWT_AUTH = { 'JWT_VERIFY_EXPIRATION': False, 'JWT_AUTH_HEADER_PREFIX': 'JWT', } REST_FRAMEWORK = { 'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.IsAuthenticated', ), 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework_jwt.authentication.JSONWebTokenAuthentication', ), 'NON_FIELD_ERRORS_KEY': 'detail', 'TEST_REQUEST_DEFAULT_FORMAT': 'json' } # Internationalization # https://docs.djangoproject.com/en/1.11/topics/i18n/ LANGUAGE_CODE = 'ja' TIME_ZONE = 'Asia/Tokyo' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.11/howto/static-files/ STATIC_URL = '/static/'

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kacchan822

2017/08/29 22:56

python manage.py dbshell としたときのDBシェル上でも各テーブルは空の状態でしょうか?
kacchan822

2017/08/29 22:57

settingsや環境(pythonバージョン、OS、Djangoバージョン、使用パッケージ)も記載していただくと回答がつきやすいと思います。
kazzzstudio

2017/08/29 23:34

dbshellでの実行結果、並びにソフトウェアバージョン、settings.pyの内容を追記しました(文字数の関係で一部省略)。引き続き、よろしくお願いいたします。
kacchan822

2017/08/29 23:51

`remosys_remoshin_user_tbl`のデータ登録状況はいかがでしょう?
sizumita

2017/08/31 13:15

python manage.py makemigrations アプリ名 してから python manage.py migrate しましたか?
kazzzstudio

2017/08/31 13:46

makemigrationsのあとにアプリ名は入れませんでした。これは入れる必要があるのでしょうか?
sizumita

2017/08/31 13:49

入れなくても大丈夫です
kacchan822

2017/08/31 21:01

user.RemoshinUser(とManagerを定義している場合はManagerも)と、user.backends.RemoshinUserAuthの内容はどうなっていますか?
kazzzstudio

2017/09/01 06:03

質問させていただいてから、コードをいじってしまいましたので、状況が変わってしまいました。うまく行かないようでしたら、また新規に質問させて頂ければと思います。ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問