in practicing using sqlite3 with django, I've created a single row via the Django Shell:
# Import our flight model
In [1]: from flights.models import Flight
# Create a new flight
In [2]: f = Flight(origin="New York", destination="London", duration=415)
# Instert that flight into our database
In [3]: f.save()
# Query for all flights stored in the database
In [4]: Flight.objects.all()
Out[4]: <QuerySet [<Flight: Flight object (1)>]>
Now I set a variable called flights to store the query:
# Create a variable called flights to store the results of a query
In [7]: flights = Flight.objects.all()
# Displaying all flights
In [8]: flights
Out[8]: <QuerySet [<Flight: 1: New York to London>]>
# Isolating just the first flight
In [9]: flight = flights.first()
Now in models.py I've done the following:
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}"
After running Migrations:
# Create New Migrations
python manage.py makemigration
# Migrate
python manage.py migrate
I get the following error, because I need to delete my existing flight from New York to London to support the new structure, BUT, I'm not sure how to do this...
Here is the error:
python manage.py migrate
System check identified some issues:
WARNINGS:
?: (urls.W005) URL namespace 'flights' isn't unique. You may not be able to
reverse all URLs in this namespace
Operations to perform:
Apply all migrations: admin, auth, contenttypes, flights, sessions
Running migrations:
Applying flights.0002_auto_20210530_1202...Traceback (most recent call last):
File "C:\Users\kaij\Documents\cs50\airline\manage.py", line 22, in <module>
main()
File "C:\Users\kaij\Documents\cs50\airline\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
utility.execute()
File "C:\Users\kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options) File "C:\Users\kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\base.py", line 398, in execute
output = self.handle(*args, **options)
File "C:\Users\kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\base.py", line 89, in wrapped
res = handle_func(*args, **kwargs) File "C:\Users\kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\commands\migrate.py", line 244, in handle
post_migrate_state = executor.migrate(
File "C:\Users\kaij\AppData\Local\Programs\Python\Python39\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\kaij\AppData\Local\Programs\Python\Python39\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\kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\migrations\executor.py",
line 230, in apply_migration
migration_recorded = True
File "C:\Users\kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\sqlite3\schema.py", line 35, in __exit__
self.connection.check_constraints()
File "C:\Users\kaij\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\sqlite3\base.py", line 353, in check_constraints, line 353, in check_constraints
raise IntegrityError( w in table 'flights_flight' with primary key '1' has an invalid foreign k
django.db.utils.IntegrityError: The rots_flight.origin_id contains a value 'New Yoesponding value in flights_aiw in table 'flights_flight' with prima value in flights_airport.id.ry key '1' has an invalid foreign key:e> flights_flight.origin_id contains a value 'New York' that does not have a corresponding value in flights_airport.id.
PS C:\Users\kaij\Documents\cs50\airline>
I've tried typing into the Django shell:
flight.delete
flight.delete()
And it still does not delete that row thanks