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.origin} To {self.destination}")
ERROR
- On running command python manage.py migrate, it shows IntegrityError.
Operations to perform: Apply all migrations: admin, auth, contenttypes, flights, sessions Running migrations: Applying flights.0002_auto_20201119_1418...Traceback (most recent call last): File "C:\Users\arora\Documents\Harvard\lec-4\airline\manage.py", line 22, in main() File "C:\Users\arora\Documents\Harvard\lec-4\airline\manage.py", line 18, in main execute_from_command_line(sys.argv) File "C:\Users\arora\Documents\Harvard\lec-4\venv\lib\site-packages\django\core\management_init_.py", line 401, in execute_from_command_line utility.execute() File "C:\Users\arora\Documents\Harvard\lec-4\venv\lib\site-packages\django\core\management_init_.py", line 395, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Users\arora\Documents\Harvard\lec-4\venv\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv self.execute(*args, **cmd_options) File "C:\Users\arora\Documents\Harvard\lec-4\venv\lib\site-packages\django\core\management\base.py", line 371, in execute output = self.handle(*args, **options) File "C:\Users\arora\Documents\Harvard\lec-4\venv\lib\site-packages\django\core\management\base.py", line 85, in wrapped res = handle_func(*args, **kwargs) File "C:\Users\arora\Documents\Harvard\lec-4\venv\lib\site-packages\django\core\management\commands\migrate.py", line 243, in handle post_migrate_state = executor.migrate( File "C:\Users\arora\Documents\Harvard\lec-4\venv\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial) File "C:\Users\arora\Documents\Harvard\lec-4\venv\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial) File "C:\Users\arora\Documents\Harvard\lec-4\venv\lib\site-packages\django\db\migrations\executor.py", line 229, in apply_migration migration_recorded = True File "C:\Users\arora\Documents\Harvard\lec-4\venv\lib\site-packages\django\db\backends\sqlite3\schema.py", line 35, in exit self.connection.check_constraints() File "C:\Users\arora\Documents\Harvard\lec-4\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 343, 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 'new york' that does not have a corresponding value in flights_airport.id.