Reputation: 6128
I'm getting a problem very strange from my point of view and I would like to get your mind.
I'm building a Django website
and I want to create a new app with his models.py
file.
I wrote this script to my Mairie models application
:
# -*- 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)
But, the two last fields (mandat1 and mandat2) don't migrate to my MySQL Database. It's pretty strange because the previous field (sex), which are written exactly the same thing works well.
I got this error :
Exception Value: (1054, "Unknown column 'mandat1' in 'field list'")
In order to migrate my models.py file, I wrote :
python manage.py makemigrations
python manage.py migrate
I'm working with Django 1.10
.
Have you an idea ?
ADDED FILES
My migration file looks like :
# -*- 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')),
],
),
]
And the command : python manage.py showmigrations
looks like :
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
Upvotes: 0
Views: 1324
Reputation: 308889
Your migrations and models have got out of sync. My guess is that you migrated with an earlier version of the 0001_initial
migration which did not create the fields.
If you don't have important data for the Mairie
app in the database, you could revert the migration and then re-run it. Warning - this will delete all data in database tables for the Mairie app. You probably shouldn't use it if other apps have foreign keys to models in the app.
python manage.py migrate Mairie zero
python manage.py migrate Mairie
If there is important data in the database which you cannot use, then you could either:
makemigrations
and migrate
again.sqlmigrate
command could help)Upvotes: 4