0

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

Katie Melosto
  • 1,047
  • 2
  • 14
  • 35

2 Answers2

1

Since you are only practicing and the integrity of the data in the current database doesn't matter I suggest doing the following:

  • Delete the db.sqlite3 file in the root of your project.
  • Find the migrations folder within your app where the flights models are and delete all of the files inside except for __init__.py.
  • Run python manage.py makemigration and python manage.py migrate again.

I find this the easiest method when situations like this occur. Do not do this in situations where you have data in your db you need to keep though.

Danoram
  • 8,132
  • 12
  • 51
  • 71
0

run the following command and the issue would be solved :

Flight.objects.filter(id=id).delete()


where: (id) is the id of the tuple that you wanna delete

Khaled
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 31 '21 at 17:56