2017-01-26 7 views
0

私の視点からは非常に奇妙な問題が発生しており、私はあなたの心を得たいと思います。Djangoはモデルフィールドを作成しません

私はDjango websiteを構築しています。彼のmodels.pyファイルで新しいアプリケーションを作成したいと思います。

私は私のMairie models applicationにこのスクリプトを書いた:2つの最後のフィールド(mandat1とmandat2は)私のMySQLデータベースに移行していない、

# -*- coding: utf-8 -*- 

from django.db import models 
from django.utils.encoding import force_text 
from django_countries.fields import CountryField 

############################################### 
# Choix à l'utilisateur pour le sexe du maire # 
############################################### 

SEX_CHOICES = (
    ('Masculin', 'Masculin'), 
    ('Feminin', 'Féminin') 
) 

TITLE_CHOICES = (
    ('Mr', 'Monsieur'), 
    ('Mlle', 'Mademoiselle'), 
    ('Mme','Madame'), 
    ('Dr','Docteur'), 
    ('Me','Maître'), 
) 

INSTITUTION_CHOICES = (
    ('Mairie','Mairie'), 
    ('Prefecture','Préfécture'), 
    ('Autre','Autre') 
) 

DEVISE_CHOICES = (
    ('EUR','EUR (€)'), 
    ('USD','USD ($)'), 
    ('CFA','CFA') 
) 

MANDAT_CHOICES = (
    ('2011','2011'), 
    ('2012','2012'), 
    ('2013','2013'), 
    ('2014','2014'), 
    ('2015','2015'), 
    ('2016','2016'), 
    ('2017','2017'), 
    ('2018','2018'), 
    ('2019','2019'), 
    ('2020','2020'), 
    ('2021','2021'), 
    ('2022','2022'), 
    ('2023','2023'), 
    ('2024','2024'), 
    ('2025','2025'), 
    ('2026','2026'), 
    ('2027','2027'), 
    ('2028','2028'), 
    ('2029','2029'), 
    ('2030','2030'), 
    ('2031','2031') 
) 

#################################################################################### 
#   Création d'une table permettant de renseigner toutes les    # 
#     informations concernant la mairie et le maire     # 
#################################################################################### 

class Mairie(models.Model): 

    institution = models.CharField(max_length=30, choices=INSTITUTION_CHOICES, null=False, verbose_name='Nom/Enseigne/Raison Sociale') 
    adress = models.CharField(max_length=30, null=False, verbose_name='Adresse') 
    zip = models.IntegerField(verbose_name='Code Postal', null=False) 
    city = models.CharField(max_length=30, verbose_name='Ville', null=False) 
    country = CountryField(blank_label='Sélectionner un pays', verbose_name='Pays', null=False) 
    department = models.CharField(max_length=30, verbose_name='Département/Canton') 
    devise = models.CharField(max_length=30, choices=DEVISE_CHOICES, null=False, verbose_name='Devise') 
    fixe = models.CharField(max_length=30, verbose_name='Téléphone Fixe', null=False) 
    fax = models.CharField(max_length=30, verbose_name='Fax', blank=True) 
    mail = models.CharField(max_length=40, verbose_name = 'Mail', null=False) 
    web = models.CharField(max_length=40, verbose_name = 'Site Web') 
    #logo = models.ImageField(upload_to='logo') 

    title = models.CharField(max_length=12,choices=TITLE_CHOICES, verbose_name='Civilité du maire', null=False) 
    lastname = models.CharField(max_length=30, verbose_name='Nom de famille du maire', null=False) 
    firstname = models.CharField(max_length=30, verbose_name='Prénom(s) du maire', null=False) 
    sex = models.CharField(max_length=8, choices=SEX_CHOICES, verbose_name='Sexe du maire', null=False) 
    mandat1 = models.CharField(max_length=8, choices=MANDAT_CHOICES, verbose_name='Début du mandat', null=False) 
    mandat2 = models.CharField(max_length=8, choices=MANDAT_CHOICES, verbose_name='Fin du mandat', null=False) 

しかし。前のフィールド(性別)はまったく同じことが書かれているのでうまくいきます。私はDjango 1.10で働いている

python manage.py makemigrations 
python manage.py migrate 

:私のmodels.pyファイルを移行するために

Exception Value: (1054, "Unknown column 'mandat1' in 'field list'") 

は、私が書いた:

私はこのエラーを得ました。

アイデアはありますか?

# -*- coding: utf-8 -*- 
# Generated by Django 1.10.2 on 2017-01-26 10:43 
from __future__ import unicode_literals 

from django.db import migrations, models 
import django_countries.fields 


class Migration(migrations.Migration): 

    initial = True 

    dependencies = [ 
    ] 

    operations = [ 
     migrations.CreateModel(
      name='Mairie', 
      fields=[ 
       ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 
       ('institution', models.CharField(choices=[(b'Mairie', b'Mairie'), (b'Prefecture', b'Pr\xc3\xa9f\xc3\xa9cture'), (b'Autre', b'Autre')], max_length=30, verbose_name=b'Nom/Enseigne/Raison Sociale')), 
       ('adress', models.CharField(max_length=30, verbose_name=b'Adresse')), 
       ('zip', models.IntegerField(verbose_name=b'Code Postal')), 
       ('city', models.CharField(max_length=30, verbose_name=b'Ville')), 
       ('country', django_countries.fields.CountryField(max_length=2, verbose_name=b'Pays')), 
       ('department', models.CharField(max_length=30, verbose_name=b'D\xc3\xa9partement/Canton')), 
       ('devise', models.CharField(choices=[(b'EUR', b'EUR (\xe2\x82\xac)'), (b'USD', b'USD ($)'), (b'CFA', b'CFA')], max_length=30, verbose_name=b'Devise')), 
       ('fixe', models.CharField(max_length=30, verbose_name=b'T\xc3\xa9l\xc3\xa9phone Fixe')), 
       ('fax', models.CharField(blank=True, max_length=30, verbose_name=b'Fax')), 
       ('mail', models.CharField(max_length=40, verbose_name=b'Mail')), 
       ('web', models.CharField(max_length=40, verbose_name=b'Site Web')), 
       ('title', models.CharField(choices=[(b'Mr', b'Monsieur'), (b'Mlle', b'Mademoiselle'), (b'Mme', b'Madame'), (b'Dr', b'Docteur'), (b'Me', b'Ma\xc3\xaetre')], max_length=12, verbose_name=b'Civilit\xc3\xa9 du maire')), 
       ('lastname', models.CharField(max_length=30, verbose_name=b'Nom de famille du maire')), 
       ('firstname', models.CharField(max_length=30, verbose_name=b'Pr\xc3\xa9nom(s) du maire')), 
       ('sex', models.CharField(choices=[(b'Masculin', b'Masculin'), (b'Feminin', b'F\xc3\xa9minin')], max_length=8, verbose_name=b'Sexe du maire')), 
       ('mandat1', models.CharField(choices=[(b'2011', b'2011'), (b'2012', b'2012'), (b'2013', b'2013'), (b'2014', b'2014'), (b'2015', b'2015'), (b'2016', b'2016'), (b'2017', b'2017'), (b'2018', b'2018'), (b'2019', b'2019'), (b'2020', b'2020'), (b'2021', b'2021'), (b'2022', b'2022'), (b'2023', b'2023'), (b'2024', b'2024'), (b'2025', b'2025'), (b'2026', b'2026'), (b'2027', b'2027'), (b'2028', b'2028'), (b'2029', b'2029'), (b'2030', b'2030'), (b'2031', b'2031')], max_length=8, verbose_name=b'D\xc3\xa9but du mandat')), 
       ('mandat2', models.CharField(choices=[(b'2011', b'2011'), (b'2012', b'2012'), (b'2013', b'2013'), (b'2014', b'2014'), (b'2015', b'2015'), (b'2016', b'2016'), (b'2017', b'2017'), (b'2018', b'2018'), (b'2019', b'2019'), (b'2020', b'2020'), (b'2021', b'2021'), (b'2022', b'2022'), (b'2023', b'2023'), (b'2024', b'2024'), (b'2025', b'2025'), (b'2026', b'2026'), (b'2027', b'2027'), (b'2028', b'2028'), (b'2029', b'2029'), (b'2030', b'2030'), (b'2031', b'2031')], max_length=8, verbose_name=b'Fin du mandat')), 
      ], 
     ), 
    ] 

とコマンド:

追加されたファイル

マイ移行ファイルは次のようになりますpython manage.py showmigrations次のようになります。

MacBook-Pro-de-Valentin:Etat_civil valentinjungbluth$ python manage.py showmigrations 
System check identified some issues: 

WARNINGS: 
?: (1_8.W001) The standalone TEMPLATE_* settings were deprecated in Django 1.8 and the TEMPLATES dictionary takes precedence. You must put the values of the following settings into your default TEMPLATES dict: TEMPLATE_DIRS. 
BirthCertificate 
[X] 0001_initial 
Identity 
[X] 0001_initial 
Mairie 
[X] 0001_initial 
admin 
[X] 0001_initial 
[X] 0002_logentry_remove_auto_add 
auth 
[X] 0001_initial 
[X] 0002_alter_permission_name_max_length 
[X] 0003_alter_user_email_max_length 
[X] 0004_alter_user_username_opts 
[X] 0005_alter_user_last_login_null 
[X] 0006_require_contenttypes_0002 
[X] 0007_alter_validators_add_error_messages 
[X] 0008_alter_user_username_max_length 
captcha 
[X] 0001_initial 
contenttypes 
[X] 0001_initial 
[X] 0002_remove_content_type_name 
log 
[X] 0001_initial 
sessions 
[X] 0001_initial 
+0

おかげでフィールドを追加 - 」 – Deadpool

+2

あなたの移行ファイル(複数可ご提示ください)と、 'python manage.py showmigrations'の出力を表示します。 – Alasdair

+0

興味深いことに、それらの( 'mandat1'と 'mandat2')は数字を含む唯一の2つのフィールド名です... – Paapaa

答えて

3

あなたの移行とモデルが同期して持っています。私の推測では、フィールドを作成しなかった0001_initial移行の以前のバージョンで移行したと思います。

データベース内のMairieアプリの重要なデータがない場合は、移行を元に戻してから再実行することができます。 警告 - これは、Mairieアプリケーションのデータベーステーブル内のすべてのデータを削除します。のアプリでモデル化するために他のアプリが外部キーを持っている場合は、おそらくそれを使用しないでください。

python manage.py migrate Mairie zero 
python manage.py migrate Mairie 

あなたが使用することはできません、データベース内の重要なデータがある場合、あなたはどちらかでした:

  • 再びmakemigrationsmigrateを実行し、移行ファイルから欠落しているフィールドを削除。
  • 手動では...私のポストを例とスクリプトとよく書かれているようだ(sqlmigrateコマンドは助けることができる)データベースには-1説明なし
+0

ありがとうございました!どちらのコマンドも動作しています(私のテーブルにはデータがありませんでした)。'makemigrations'と' migrate'を行う前にマイグレーションファイルを削除したので、かなり変です。 – Deadpool

+0

マイグレーションファイルをマイグレーションした後で、マイグレーションファイルを削除しないでください。新しい移行ファイルは、データベースと同期しなくなります。新しいフィールドを追加したら、削除せずに 'makemigrations'を実行してください。 Djangoは新しいフィールドを追加した '0002'移行を作成しました。しばらくすると、移行が多すぎる場合は、[squash](https://docs.djangoproject.com/en/1.10/topics/migrations/#squashing-migrations)することができます。 – Alasdair

+0

私は 'squashmigration'を知りませんでした。私はおそらく5または6ファイルを持っていて、他のマイグレーションを行うことができなかったので、マイグレーションファイルを削除しました。あなたのアドバイスをありがとう;) – Deadpool

関連する問題