admin管理员组

文章数量:1336398

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.

When I run python manage.py migrate this appears.

This is the code:

def main():
    """Run administrative tasks."""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'airline.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "fet to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()

I tried to write another value for New York but it doesn't work either

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.

When I run python manage.py migrate this appears.

This is the code:

def main():
    """Run administrative tasks."""
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'airline.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "fet to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()

I tried to write another value for New York but it doesn't work either

Share Improve this question edited Nov 20, 2024 at 17:59 Compo 38.7k5 gold badges31 silver badges45 bronze badges asked Nov 20, 2024 at 8:14 user28239646user28239646 1 2
  • 3 Please do not upload images of code/data/errors. – MatBailie Commented Nov 20, 2024 at 8:26
  • 1 The error message says it all. You tried to insert a flight from a non-existing airport. – jarlh Commented Nov 20, 2024 at 8:27
Add a comment  | 

1 Answer 1

Reset to default -1

It seems the origin_id column in the flights_flight table references the primary key of the flights_airport table. The value 'New York' in flights_flight.origin_id does not correspond to any valid id in the flights_airport table. So, ensure your foreign key relationship code in your modles.py, or check your data in your flights_flight table, maybe delete the row where flights_flight.origin_id = 'New York'

本文标签: pythonwhy SGL give me djanfodbutilsintegrityError idk what that meansStack Overflow