django.db.utils.OperationalError: foreign key mismatch - "pet_profile_petphoto_pet" referencing "pet_profile_petphoto"

668 views
Skip to first unread message

Michael Starr

unread,
Mar 1, 2023, 3:33:00 PM3/1/23
to Django users
I have an error when I migrate my models. makemigrations passes just fine, but upon migrate, it seems to think there is some error in the pet_photo class' pet parameter and the pet_photo class:
class Pet(models.Model):
    slug = models.SlugField(max_length = 5, primary_key = True, blank = True, null=False)
    name = models.CharField(max_length = 255, unique = False)
    id = models.CharField(max_length = 261, unique = True, default = uuid.uuid1)
    animaltype = models.CharField(choices = ANIMALTYPE_CHOICES, max_length = 255, default="the one that barks")
    age = models.PositiveIntegerField()
    profile_photo = models.ImageField(blank = True)

    def save(self, *args, **kwargs):  # new
        slug_save(self)
        get_ID(self)
        return super().save(*args, **kwargs)

    def __str__(self):
        return self.name

class PetPhoto(models.Model):
    slug = models.SlugField(max_length = 5, primary_key = True, blank = True, null=False)
    title = models.CharField(max_length = 255)
    id = models.CharField(max_length = 261, default=uuid.uuid1)
    pet = models.ManyToManyField(Pet, related_name="Pet_Photos")
    photo = models.ImageField(blank = False)

    def save(self, *args, **kwargs):  # new
        slug_save(self)
        get_ID(self)
        return super().save(*args, **kwargs)

    def __str__(self):
        return self.title

I don't see what's wrong. I have a manytomany field where each photo is manytomany to any number of pets (I don't know the correct terminology for this, if someone could correct my grammar please do so). But so what? I have manytomanyfield in other area of models:

class PetOwner(models.Model):
    slug = models.SlugField(max_length = 5, primary_key = True, blank = True, null=False)
    name = models.CharField(max_length = 255)
    id = models.CharField(max_length = 261, unique = True, default=uuid.uuid1)
    age = models.PositiveIntegerField()
    location = models.CharField(max_length = 255)
    profile_photo = models.ImageField(blank = True)
    pets = models.ManyToManyField(Pet, related_name = "Owners")

    def get_absolute_url(self):
        return reverse("owner_profile", kwargs={"slug": self.slug})  

    def save(self, *args, **kwargs):  # new
        slug_save(self)
        get_ID(self)
        return super().save(*args, **kwargs)

    def __str__(self):
        return self.name

and no error ever.

Most of the google search results say to delete the migration .pyc files under the migrations/ folder buuuuuut they don't seem relevant. It's just like, added this parameter, changed this variable. There's nothing that really points to the manytomany relationship between pet and pet_photo in there, is what I am saying. So I don't see how deleting them (after rolling back the migrations, of course--though I think it will automatically reconstruct them either way so you don't have to roll them back per se before deleting them) would do anything. I tried deleting the most recent three and redid the database migrations and nothing worked.

I'm stuck on this one. Google is not being too useful.

Any help is appreciated.

Michael

Michael Starr

unread,
Mar 1, 2023, 3:35:59 PM3/1/23
to Django users
I even deleted the pet field from pet_photo and it still gives this error, even after deleting the db.sqlite3 database and remigrating. Makes no sense whatsoever. The error is now referencing something that is not even there!
Michael

Michael Starr

unread,
Mar 2, 2023, 5:01:15 PM3/2/23
to Django users
This
seems to work, but now I am getting a slew of database table model errors.

Michael Starr

unread,
Mar 2, 2023, 5:02:36 PM3/2/23
to Django users
Including not even finding the basic model field Pet, the most basic thing
OperationalError at /admin/pet_profile/pet/ no such table: pet_profile_pet
It's totally effed.

Michael

Michael Starr

unread,
Mar 2, 2023, 5:19:52 PM3/2/23
to Django users
Now I am trying
But it doesn't get rid of
OperationalError at /admin/pet_profile/pet/ no such table: pet_profile_pet

Django is acting as though it has hidden memory somewhere, outside of the db.sqlite database and the migration files. I deleted all of them, redid the makemigration command, redid migrate, and there was some text, namely

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK

So it seems to have worked.

But the error, operationalerror no such table, is still there.

I have to recreate the superuser, as a side note, but I don't think this is relevant.

Leaning toward posting this in stackoverflow soon. Will take some time to edit, polish, and edit again and again the question so that it is up to the perfectionist standards of the stackoverflow tzars.

Michael

Michael Starr

unread,
Mar 3, 2023, 5:01:03 PM3/3/23
to Django users
I fixed it. Let me see if I can find the stack article that helped...
Well anyway, the command that did it was
python manage.py migrate --run-syncdb
after deleting the pycache files, the db.sqlite file, and the migration files.

So delete all those, then run migrate --run-syncdb.
That did an ACTUAL migration.
Nifty command to know.

Michael
Reply all
Reply to author
Forward
0 new messages