Django does not create the following model.

from django.db import models from django.contrib.auth.models import User class Client(models.Model): user=models.ForeignKey(User,on_delete=models.CASCADE) contact_url=models.URLField(max_length=100) 

And no error appears (see picture)

enter image description here And the strangest thing is that he creates other models, for example:

 class Doctor(models.Model): name=models.CharField(max_length=100) 

Also I enter the python manage.py makemigrations app and python manage.py migrate

The model is successfully created and the corresponding table for it in the database app_doctor

enter image description here

I also tried to use the OneToOneField relation instead of the ForeignKey , but this did not give a result. Although according to official documentation it works fine. Tell me, what could be the matter?

  • Let me guess, have you once created migrations for Client, applied and then deleted? - andreymal
  • Yes, there was such a thing. I created migrations, applied - they did not work. Again deleted and applied. - Vladimir Goncharuk
  • And when you created the Doctor model, what was the name of the migration? - andreymal
  • Next 0002_doctor.py . When the client model was created, the name was 0001_initial.py - Vladimir Goncharuk
  • Well, then everything is clear - andreymal

1 answer 1

This happens if you create and apply migrations, then delete, not roll back, and recreate it with the same name. Django stores information about all applied migrations in the database, and it has already been recorded there that the application has already been executed, and the application has already been done, and there is no need to do it again. And about the migration of 0002_doctor there was no such record, and therefore it was fulfilled. Since you are not using the native rollback mechanism (through the same migrate command), you need to manually clear the migration information.

To delete information about all migrations of an application (in your case, the имя прилоТСния is app ):

 delete from django_migrations where app = 'имя прилоТСния' 

To remove one specific migration (where 0001_initial is its name):

 delete from django_migrations where app = 'имя прилоТСния' and name = '0001_initial' 

After that, the migrate command should earn and apply the newly created migration.

Note that this is all at your own peril and risk, and if you delete the migrations manually, do not roll them back as expected (for example, without deleting the table that the migration created), then the base may turn out to be bad and glitches may pop up. Watch what you are doing.

  • and name = '0001_initial' Optional. And sometimes it's even harmful :) - Mikhail Alekseevich