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

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

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

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

Model

MVCモデルの一部であるModelはアプリケーションで扱うデータとその動作を管理するために扱います。

継承

継承(インヘリタンス)はオブジェクト指向プログラミングに存在するシステムです。継承はオブジェクトが各自定義する必要をなくし、継承元のオブジェクトで定義されている内容を引き継ぎます。

Python

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

Q&A

解決済

1回答

3441閲覧

makemigrationsでdefaultをtimezone.nowとしたらmigrate出来なくなってしまいました。

prairie

総合スコア6

Django

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

Model

MVCモデルの一部であるModelはアプリケーションで扱うデータとその動作を管理するために扱います。

継承

継承(インヘリタンス)はオブジェクト指向プログラミングに存在するシステムです。継承はオブジェクトが各自定義する必要をなくし、継承元のオブジェクトで定義されている内容を引き継ぎます。

Python

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

0グッド

0クリップ

投稿2020/07/31 19:15

前提・実現したいこと

migrate時のエラーを解決したいです。
あとmakemigrationsしたときに以下のような指示が出てきた場合、どう処理するのが良いのかを教えていただきたいです。

発生している問題・エラーメッセージ

まずmakemigrationsをしたときに以下のような指示が出てきたのであまりよく考えずにdefaultをtimezone.nowに設定しました。(本当にそうしているのかもよくわかっていないです。)

$ python3 manage.py makemigrations You are trying to change the nullable field 'auther' on mahjong_uma_oka_model to non-nullable 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) Ignore for now, and let me handle existing rows with NULL myself (e.g. because you added a RunPython or RunSQL operation to handle NULL values in a previous data migration) 3) Quit, and let me add a default in models.py Select an option: 1 Please enter the default value now, as valid Python The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now Type 'exit' to exit this prompt >>> timezone.now You are trying to change the nullable field 'auther' on mahjongplayermodel to non-nullable 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) Ignore for now, and let me handle existing rows with NULL myself (e.g. because you added a RunPython or RunSQL operation to handle NULL values in a previous data migration) 3) Quit, and let me add a default in models.py Select an option: 1 Please enter the default value now, as valid Python The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now Type 'exit' to exit this prompt >>> timezone.now You are trying to change the nullable field 'auther' on mahjongrecordmodel to non-nullable 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) Ignore for now, and let me handle existing rows with NULL myself (e.g. because you added a RunPython or RunSQL operation to handle NULL values in a previous data migration) 3) Quit, and let me add a default in models.py Select an option: 1 Please enter the default value now, as valid Python The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now Type 'exit' to exit this prompt >>> timezone.now Migrations for 'mahjongrecord': mahjongrecord/migrations/0017_auto_20200731_1824.py - Alter field auther on mahjong_uma_oka_model - Alter field auther on mahjongplayermodel - Alter field auther on mahjongrecordmodel

そして続けてmigrateしたところエラーが発生してしまいました。

ryu1maniwa@MyComputer:/mnt/c/Users/ryu1m/Desktop/Django 開発/mahjongproject_vol5_user$ python3 manage.py migrate ・ ・ ・ TypeError: Field 'id' expected a number but got datetime.datetime(2020, 7, 31, 18, 24, 47, 215187, tzinfo=<UTC>).

###ソースコード
manage.pyに以下のようなget_user_model()を継承するmodels.ForeignKeyを3つ作成しました。
#####manage.py

python

1class MahjongPlayerModel(models.Model): 2 auther = models.ForeignKey( 3 get_user_model(), 4 on_delete=models.CASCADE, 5 ) 6 player1 = models.CharField(max_length = 10, default = 'player1') 7 player2 = models.CharField(max_length = 10, default = 'player2') 8 player3 = models.CharField(max_length = 10, default = 'player3') 9 player4 = models.CharField(max_length = 10, default = 'player4')

試したこと

autherのdefault=0にしてみて再度makemigrationsしてみる
autherの情報を消去してもともとmigrate出来ていた状態に戻して再度makemigrations, migrateしてみる

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答1

0

ベストアンサー

新しく項目を作ったのに、既存のレコードの付与するdefaultが記載されていなかったため、

makemigrationsをしたときに以下のような指示が...

になりました。で、ForeignKeyなのでid(数値)を打ち込むはずなのですがtimezone.now()としてしまったため

TypeError: Field 'id' expected a number but got...

数値じゃなくてdatetimeが返ってきたと怒られました。


解決策
一回MahjongPlayerModelをコメントアウトして既存のテーブルを削除し、makemigrations, migrateを行う。そしてからコメントアウトを外し、再度makemigrations, migrateする。再度行うときは、defaultはいりません。

投稿2020/07/31 19:56

ForestSeo

総合スコア2720

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

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

prairie

2020/08/01 06:04 編集

回答ありがとうございます。 テーブルを削除するとこまではできたのですが再度migrateを行うときたくさんのエラーが出てしまったので、 autherというfieldを入れる前にバックアップを取っていたものからやり直すことにしました。 そこで、同じようにしてmakemigrationsしたところ以下のような指示が出てきました。(上のと同じ) そこで今度はdefault=0としてみたら正常にmigrateが行われ、エラーは解決できました。ありがとうございます。 $ python3 manage.py makemigrations You are trying to add a non-nullable field 'auther' to mahjong_uma_oka_model 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: 1 Please enter the default value now, as valid Python The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now Type 'exit' to exit this prompt >>> 0 また、default=0としたらget_user_model()をautherが継承する際、defaultではid=0のものを継承するという認識は正しいでしょうか。
ForestSeo

2020/08/01 06:15 編集

はい。デフォルトがid=0になります でも、id=0って何なんでしょうね...
ForestSeo

2020/08/01 06:16

>テーブルを削除するとこまではできたのですが再度migrateを行うときたくさんのエラーが出てしまったので、 本当ですか? 削除してから、makemigrations, migrate は行いましたか?
prairie

2020/08/01 16:57

テーブルを削除するというのはModelをコメントアウトすることという認識であっていますか? Modelをコメントアウトし、makemigrationsしたところurls.pyやviews.pyでコメントアウトしたモデルを使用していたためエラーが起きてしまい、urls.pyやviews.pyもコメントアウトすることでそのエラーを解決したらmakemigrationsできてModelを削除することはできました。しかしmigrateしようとすると質問した時と同じ TypeError: Field 'id' expected a number but got datetime.datetime(2020, 7, 31, 18, 24, 47, 215187, tzinfo=<UTC>). というエラーが出てしまいました。
ForestSeo

2020/08/01 19:34

あーなるほど。 じゃあ無理かもしれません。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問