It happens to me with some frequency that when I enter a field in the models file, the field is processed in makmigratione but not in mgration and I am forced to enter it manually of the database
Am I doing something wrong ?
Possibly.
But without seeing all the details, it’s going to be tough to diagnose this.
If you can show:
- A “before” and “after” of one example of one model (the complete model, not just the field being changed) where this is happening,
- The complete output of the
makemigration
command being run, - The migration file being generated,
- The complete output of the
migrate
command,
and - Your
DATABASES
setting
we might be able to help identify the source of the issue.
Before posting the code I deleted all the migration files and launched makemigration
The affected table is Customers, place only that one and not all the tables
Models before adding the field
class Customers(models.Model):
Master data = models.CharField(max_length=50, blank=False, null=False)
# Gender field with choice between "M" and "F"
Gender = models.CharField(max_length=1, choices=[('M', 'Male'), ('F', 'Female')], default='', blank=False)
BirthDate = models.DateField(default=date.today)
ComuneNascitaID = models.ForeignKey(Comuni, on_delete=models.PROTECT, null=True, related_name='ComuneNascClientID')
Address = models.CharField(max_length=30, default='')
ComuneID = models.ForeignKey(Comuni, on_delete=models.PROTECT, null=True, related_name='ComuneResiClientID')
Cap = models.IntegerField(validators=[MaxValueValidator(99999), MinValueValidator(0)], blank=True, null=True)
Photo = models.ImageField(upload_to='Customers/', blank=True, default='Site/Mamozio.png')
TaxCode = models.CharField(max_length=16, default='', blank=True, null=True)
PartitaVAT = models.IntegerField(null=True, blank=True)
BankID = models.ForeignKey(Banks, on_delete=models.PROTECT, null=True, blank=True)
Phone = models.IntegerField(null=True, blank=True)
Cell = models.IntegerField(null=True, blank=True)
eMail = models.EmailField(max_length=50, default='', blank=True, null=False)
PEC = models.EmailField(max_length=50, default='', blank=True, null=False)
DateFirstIscr = models.DateField(default=date.today)
MedicalCertExpire = models.DateField(default=date.today)
CustomerType = models.ForeignKey(CustomerType, on_delete=models.PROTECT, null=False, blank=False)
Convention = models.ForeignKey(Conventions, on_delete=models.PROTECT, null=True, blank=True)
CertMedicoASI = models.DateField(default=date.today)
DisciplineASIID = models.ForeignKey(ASIDiscipline, on_delete=models.PROTECT, null=True, blank=True)
class Meta:
# db_table = 'Customers'
ordering = ['Personal data']
verbose_name = 'Customers'
verbose_name_plural = 'Customer Management'
unique_together = ['Anagrafica', 'CodFiscale', 'PartitaVAT']
# permissions = ['Edit_all'] # controls what you can enter
default_permissions = ('add', 'change', 'delete', 'view')
@property
def prov_abi(self):
if self.ComuneID:
return self.ComuneID.Prov, self.BankID.ABI
return None, None
After makemigration no errors are returned
I attach the migration file, again for the Customer Table only
migrations.CreateModel(
name='Customers',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('Personal data', models.CharField(max_length=50)),
('Sex', models.CharField(choices=[('M', 'Male'), ('F', 'Female')], default='', max_length=1)),
('DateBirth', models.DateField(default=datetime.date.today)),
('Address', models.CharField(default='', max_length=30)),
('Cap', models.IntegerField(blank=True, null=True, validators=[django.core.validators.MaxValueValidator(99999), django.core.validators.MinValueValidator(0)])),
('Photo', models.ImageField(blank=True, default='Site/Mamozio.png', upload_to='Collaborators/')),
('CodFiscale', models.CharField(blank=True, default='', max_length=16, null=True)),
('PartitaIva', models.IntegerField(blank=True, null=True)),
('Phone', models.IntegerField(blank=True, null=True)),
('Cellular', models.IntegerField(blank=True, null=True)),
('eMail', models.EmailField(blank=True, default='', max_length=50)),
('PEC', models.EmailField(blank=True, default='', max_length=50)),
('DateFirstIscr', models.DateField(default=datetime.date.today)),
('CertMedicoScade', models.DateField(default=datetime.date.today)),
('CertMedicoASI', models.DateField(default=datetime.date.today)),
('BankID', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='Pale.banks')),
('DisciplineASIID', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='Pale.asidiscipline')),
('CustomerType', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='Pale.customertype')),
('ComuneID', models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, related_name='ComuneResiClientID', to='Pale.comuni')),
('ComuneNascitaID', models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, related_name='ComuneNascClientID', to='Pale.comuni')),
('Convention', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='Pale.conventions')),
],
options={
'verbose_name': 'Customers',
'verbose_name_plural': 'Customer Management',
'ordering': ['Personal data'],
'default_permissions': ('add', 'change', 'delete', 'view'),
'unique_together': {('Anagrafica', 'CodFiscale', 'VAT number')},
},
),
I launch the migrate and everything is fine, the message returns: Operations to perform:
Apply all migrations: Pale, Site, admin, auth, contenttypes, sessions
Running migrations:
No migrations to apply.
I add the field: Curricolo = models.FileField(upload_to=‘Curricolo/’), immediately after Photo
CustomerTable with new field
class Customers(models.Model):
Master data = models.CharField(max_length=50, blank=False, null=False)
# Gender field with choice between "M" and "F"
Gender = models.CharField(max_length=1, choices=[('M', 'Male'), ('F', 'Female')], default='', blank=False)
BirthDate = models.DateField(default=date.today)
ComuneNascitaID = models.ForeignKey(Comuni, on_delete=models.PROTECT, null=True, related_name='ComuneNascClientID')
Address = models.CharField(max_length=30, default='')
ComuneID = models.ForeignKey(Comuni, on_delete=models.PROTECT, null=True, related_name='ComuneResiClientID')
Cap = models.IntegerField(validators=[MaxValueValidator(99999), MinValueValidator(0)], blank=True, null=True)
Photo = models.ImageField(upload_to='Collaborators/', blank=True, default='Site/Mamozio.png')
Resume = models.FileField(upload_to='Collaborators/', null=True)
TaxCode = models.CharField(max_length=16, default='', blank=True, null=True)
PartitaVAT = models.IntegerField(null=True, blank=True)
BankID = models.ForeignKey(Banks, on_delete=models.PROTECT, null=True, blank=True)
Phone = models.IntegerField(null=True, blank=True)
Cell = models.IntegerField(null=True, blank=True)
eMail = models.EmailField(max_length=50, default='', blank=True, null=False)
PEC = models.EmailField(max_length=50, default='', blank=True, null=False)
DateFirstIscr = models.DateField(default=date.today)
MedicalCertExpire = models.DateField(default=date.today)
CustomerType = models.ForeignKey(CustomerType, on_delete=models.PROTECT, null=False, blank=False)
Convention = models.ForeignKey(Conventions, on_delete=models.PROTECT, null=True, blank=True)
CertMedicoASI = models.DateField(default=date.today)
DisciplineASIID = models.ForeignKey(ASIDiscipline, on_delete=models.PROTECT, null=True, blank=True)
class Meta:
# db_table = 'Customers'
ordering = ['Personal data']
verbose_name = 'Customers'
verbose_name_plural = 'Customer Management'
unique_together = ['Anagrafica', 'CodFiscale', 'PartitaVAT']
# permissions = ['Edit_all'] # controls what you can enter
default_permissions = ('add', 'change', 'delete', 'view')
@property
def prov_abi(self):
if self.ComuneID:
return self.ComuneID.Prov, self.BankID.ABI
return None, None
def __str__(self):
return self.Personal data
I launch makemigrate and this is the contents of the file
Generated by Django 5.0.1 on 2024-04-29 08:02
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('Pale', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='customers',
name='Curriculum',
field=models.FileField(null=True, upload_to='Contributors/'),
),
]
I launch mgrate and this message comes back
Operations to perform:
Apply all migrations: Pale, Site, admin, auth, contenttypes, sessions
Running migrations:
No migrations to apply.
I manually check the database, but the field is not there
This happens to me often and I’m forced to add it manually.
I don’t understand what I’m doing wrong
What are your DATABASES
setting? What database engine are you using? Is this a case where multiple databases are involved?
What is the output of a showmigrations
command?
Ok, this is major mistake #1. Never delete migrations files, unless you understand exactly what you’re doing, and “why”.
- At this point, the easiest solution is to drop and recreate your database.
It can be fixed, but could require some effort. I’ve generally found it easier to dump the data needing to be saved, drop and recreate the database, then load the data back in. It generally takes less time to do that than ensuring your models and database are in sync with the migration table. - You’ll also want to ensure that you have deleted any files that may still be in the
migrations/__pycache__
directory.
Thanks for the reply but I did it as I had read somewhere that in case of failure it was convenient to cncell the migration files and relaunch it.
If I understand correctly I have to delete completely, the contents of the pycache folder, delete the dataBase, create it and then load only the data
Rephrasing slightly, adding some additional details:
Most importantly, you don’t want to make any model changes during this process, to ensure that the data you are saving in step #1 can be cleanly reloaded in step #8.
Once you have completed this, you should then be able to make your model changes, and have the migration system recognize and apply them as needed.
(And in the future, do not delete any migrations that have been applied, without understanding the implications and what you may need to do to fix the resulting issues.)
I just now finished importing the data.
Only a few tables I could not copy like content_type and others but I let it go as the system had created them directly
Now there is also the field that it did not create
I will follow your advice and thank you