Django database migration

I’ve created a model and wanted to apply the migration but my code rised this erro.

from django.db import models

Create your models here.

class Airport(models.Model):
code = models.CharField(max_length=3)
city = models.CharField(max_length=64)

def __str__(self):
    return f"{self.city} ({self.code})"

class Flight(models.Model):
origin = models.ForeignKey(Airport, on_delete=models.CASCADE, related_name=“departures”)
destination = models.ForeignKey(Airport, on_delete= models.CASCADE, related_name=“arrivals”)
duration = models.IntegerField()

def __str__(self):
    return f"{self.id} {self.origin} to {self.destination}" 
 raise IntegrityError(
django.db.utils.IntegrityError: The row in table 'flights_flight' with primary key '1' has an invalid foreign key: flights_flight.origin_id contains a value 'Newyork' that does not have a corresponding value in flights_airport.id.

Are you using a database with populated fields?

Sorry for the question but What does populated fields mean?

Are there any rows on the table that this migration is being applied?

Yes. I created two tables one for the flights and the other for the Airport. So the flight one is applied.

Looking at the error message, what do you that it’s telling you?

 "C:\Python311\Lib\site-packages\django\db\backends\sqlite3\base.py", line 264, in check_constraints
    raise IntegrityError(
django.db.utils.IntegrityError: The row in table 'flights_flight' with primary key '1' has an invalid foreign key: flights_flight.origin_id contains a value 'Newyork' that does not have a corresponding value in flights_airport.id.

This is the error message on the terminal. when ever i try to migrate. this error is raised.

I already seen the message.
I’m asking you, what do you think that the message is telling to you?

raise IntegrityError(
django.db.utils.IntegrityError: The row in table ‘flights_flight’ with primary key ‘1’ has an invalid foreign key: flights_flight.origin_id contains a value ‘Newyork’ that does not have a corresponding value in flights_airport.id.

This is the error message. I could figure out the exact error but i think there is mismatch in my db tables.

You probably:

  • Changed a field’s default;
  • Changed a field’s pk;
  • Deleted some registers, and on django on the foreignkey you had on_delete=models.DO_NOTHING

If your application is only on development at the moment, meaning that you don’t have a production database with migrations applied, one easy way to solving this is:
1 - Delete the development database;
2 - Delete the migrations files;
3 - Run makemigrations again;
4 - Run migrate;

If you have a application that contains a production database, then you might need to:
1 - Unapply the migrations applied to your development database with the django-admin command migrate zero;
2 - Apply the migrations again

1 Like

Thanks, solved the issue. I deleted all the entries and then applied the migration again and worked well.

To complete the Airport model, you’ll need to import the models module from Django. Here’s how you can do that:

Copy code

from django.db import models

class Airport(models.Model):
    code = models.CharField(max_length=3)
    city = models.CharField(max_length=64)

The CharField is a field that allows you to store character data in your database. In this case, the code field has a maximum length of 3 characters, and the city field has a maximum length of 64 characters.

Keep in mind that this is just a basic example of a Django model. There are many other field types that you can use, such as IntegerField, DateTimeField, and so on. You can also specify additional options for each field, such as whether it’s required or unique.

Carlotta Krajcik
Microsoft Solution Assessment