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

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

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

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

2652閲覧

OperationalError at /admin/polls/question/

退会済みユーザー

退会済みユーザー

総合スコア0

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2017/08/08 12:59

OperationalError at /admin/polls/question/
no such column: polls_question.question_text
とエラーが出ました。
Tracebackでは

The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/Users/XXX/django/django/core/handlers/exception.py", line 35, in inner response = get_response(request) File "/Users/XXX/django/django/core/handlers/base.py", line 130, in _get_response response = self.process_exception_by_middleware(e, request) File "/Users/XXX/django/django/core/handlers/base.py", line 128, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/Users/XXX/django/django/contrib/admin/options.py", line 555, in wrapper return self.admin_site.admin_view(view)(*args, **kwargs) File "/Users/XXX/django/django/utils/decorators.py", line 142, in _wrapped_view response = view_func(request, *args, **kwargs) File "/Users/XXX/django/django/views/decorators/cache.py", line 44, in _wrapped_view_func response = view_func(request, *args, **kwargs) File "/Users/XXX/django/django/contrib/admin/sites.py", line 224, in inner return view(request, *args, **kwargs) File "/Users/XXX/django/django/utils/decorators.py", line 62, in _wrapper return bound_func(*args, **kwargs) File "/Users/XXX/django/django/utils/decorators.py", line 142, in _wrapped_view response = view_func(request, *args, **kwargs) File "/Users/XXX/django/django/utils/decorators.py", line 58, in bound_func return func.__get__(self, type(self))(*args2, **kwargs2) File "/Users/XXX/django/django/contrib/admin/options.py", line 1658, in changelist_view selection_note=_('0 of %(cnt)s selected') % {'cnt': len(cl.result_list)}, File "/Users/XXX/django/django/db/models/query.py", line 223, in __len__ self._fetch_all() File "/Users/XXX/django/django/db/models/query.py", line 1098, in _fetch_all self._result_cache = list(self._iterable_class(self)) File "/Users/XXX/django/django/db/models/query.py", line 53, in __iter__ results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size) File "/Users/XXX/django/django/db/models/sql/compiler.py", line 1001, in execute_sql cursor.execute(sql, params) File "/Users/XXX/django/django/db/backends/utils.py", line 77, in execute return super().execute(sql, params) File "/Users/XXX/django/django/db/backends/utils.py", line 62, in execute return self.cursor.execute(sql, params) File "/Users/XXX/django/django/db/utils.py", line 89, in __exit__ raise dj_exc_value.with_traceback(traceback) from exc_value File "/Users/XXX/django/django/db/backends/utils.py", line 62, in execute return self.cursor.execute(sql, params) File "/Users/XXX/django/django/db/backends/sqlite3/base.py", line 303, in execute return Database.Cursor.execute(self, query, params) django.db.utils.OperationalError: no such column: polls_question.question_text

pollsアプリの中で

from django.db import models # Create your models here. class Question(models.Model): question_text = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') class Choice(models.Model): question = models.ForeignKey(Question,on_delete=models.CASCADE) choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0)

とコードを書きました。
そして、

./manage.py makemigrations

とコマンドをうち
(You are trying to add a non-nullable field 'pub_date' to question without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:

  1. Provide a one-off default now (will be set on all existing rows with a null value for this column)
  2. Quit, and let me add a default in models.py

Select an option: 2 と出たときに2を選択)

さらに

./manage.py migrate

とコマンドをうちました。
そのときに、

Operations to perform: Apply all migrations: admin, auth, contenttypes, polls, sessions Running migrations: No migrations to apply. Your models have changes that are not yet reflected in a migration, and so won't be applied. Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply the

と出ました。
それから

./manage.py runserver

としてサーバーを立ち上げました。

./manage.py makemigrations

./manage.py migrate

のmigrateでなぜ2種類のコマンドがあるのかが理解できていませんが、
なぜ/admin/polls/question/ のurlができていないのかわかりません。
どう直せば良いのでしょうか?

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

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

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

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

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

guest

回答1

0

ベストアンサー

python

1(You are trying to add a non-nullable field 'pub_date' to question without a default; we can't do that (the database needs something to populate existing rows). 2Please select a fix: 31) Provide a one-off default now (will be set on all existing rows with a null value for this column) 42) Quit, and let me add a default in models.py 5Select an option: 2 と出たときに2を選択)

この表示されている内容ですが、
「'pub_date'っていう空欄が許されないフィールドをデフォルトの値なしで追加しようとしてるけど、既に追加しているデータに何らかの値を入れないとまずいよ。どうする?

  1. デフォルトの値を入れる
  2. 一旦終了して、models.pyを修正するよ


といった感じの内容です。
モデルを作成しDBに反映した後に、フィールドを修正・追加するときによく見かける内容です。
(一度makemigrationsとmigrateしてから、pub_dateを新しく追加したりしませんでしたか?)

既にDBに登録されているデータにはpub_dateというフィールドはまだないので、それをなんとかしないといけません。
大まかな解決策は2つあり、以下のようにpub_dateが空欄でも良いように変更するか

python

1class Question(models.Model): 2 question_text = models.CharField(max_length=200) 3 pub_date = models.DateTimeField('date published', null=True, blank=True) 4

もしくはデフォルトの値を入れる必要があります。
このとき、データの型には気をつけてください。timezone.nowは、そのタイムゾーンでの今現在の日付時間になります。

python

1from django.utils import timezone 2 3class Question(models.Model): 4 question_text = models.CharField(max_length=200) 5 pub_date = models.DateTimeField('date published', default=timezone.now)

どちらかにしたあと、makemigrations と migrateを行ってみてください。

考えるのが面倒であれば、とりあえず空欄でも良いような設定にしておくと良いです。
その後、空欄許可はまずいなーと思ったら全てのデータのpub_dateを入力し、null=True, blank=Trueを消してmakemigrations migrateをすると良いです。

投稿2017/08/08 13:52

toritoritorina

総合スコア972

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問