1

I have this Error :

IntegrityError at /api/post_flight_schedule/
NOT NULL constraint failed: flights_tailnumber.aircraft_type_id

When I try to add a new PosFlightSchedule object to DB over http://127.0.0.1:8000/api/pos_flight_schedule (Website/APIView)

I have the below serializer :

class PosFlightScheduleModelSerializer(ModelSerializer):
    class Meta:
        model = PosFlightSchedule
        fields = ['pos_route_id', 'tail_number', 'pos_flight_number', 'pos_flight_departure_time', 'pos_flight_date',
              'pax_count']


class PosFlightScheduleSerializer(serializers.Serializer):
    pos_route_id = serializers.CharField(source='pos_route_id.route_id', read_only=False)
    tail_number = serializers.CharField(source='tail_number.tail_number', read_only=False)
    pos_flight_number = serializers.CharField(source='pos_flight_number.flight_number', read_only=False)
    pos_flight_departure_time = serializers.CharField(source='pos_flight_departure_time.flight_departure_time', allow_null=True,
                                                  read_only=False)
    pos_flight_date = serializers.CharField(source='pos_flight_date.flight_date', read_only=False)
    pax_count = serializers.IntegerField(read_only=False)

    def create(self, validated_data):
        tail_number_data = validated_data.pop("tail_number")
        tail_number = TailNumber.objects.create(**tail_number_data)
        flight_number_data = validated_data.pop("pos_flight_number")
        flight_number = FlightSchedule.objects.create(**flight_number_data)
        flight_departure_time_data = validated_data.pop("pos_flight_departure_time")
        print "DEP_TIME" + str(flight_departure_time_data)
        flight_departure_time = FlightSchedule.objects.create(**flight_departure_time_data)
        route_id_data = validated_data.pop("pos_route_id")
        route_id = FlightScheduleDetail.objects.create(**route_id_data)
        flight_date_data = validated_data.pop("pos_flight_date")
        flight_date = FlightScheduleDetail.objects.create(**flight_date_data)
        pax_count = validated_data.pop("pax_count")

        schedule_obj = PosFlightSchedule.objects.create(**validated_data)
        # if tail_number:
        schedule_obj.set_tail_number(tail_number)
        schedule_obj.set_pos_flight_number(flight_number)
        schedule_obj.set_pos_flight_departure_time(flight_departure_time)
        schedule_obj.set_pos_route_id(route_id)
        schedule_obj.set_pos_flight_date(flight_date)
        schedule_obj.set_pax_count(pax_count)
        schedule_obj.save()

        return schedule_obj

    def update(self, instance, validated_data):
        tail_number = validated_data.pop("tail_number")
        flight_number = validated_data.pop("pos_flight_number")
        flight_departure_time = validated_data.pop("pos_flight_departure_time")
        route_id = validated_data.pop("pos_route_id")
        flight_date = validated_data.pop("pos_flight_date")
        pax_count = validated_data.pop("pax_count")
        instance.__dict__.update(validated_data)
        if tail_number:
            instance.set_tail_number(tail_number)
        if flight_number:
            instance.set_pos_flight_number(flight_number)
        if flight_departure_time:
            instance.set_pos_flight_departure_time(flight_departure_time)
        if route_id:
            instance.set_pos_route_id(route_id)
        if flight_date:
            instance.set_pos_flight_date(flight_date)
        if pax_count:
            instance.set_pax_count(pax_count)
        instance.save()
        return instance

The model of the field which is giving error looks like :

class TailNumber(models.Model):
    tail_number_id = models.AutoField(null=False, primary_key=True)
    tail_number = models.CharField(max_length=20, null=False, blank=False, unique=True)
    aircraft_type = models.ForeignKey(AircraftType, null=False, blank=False)

    def __unicode__(self):
        return u'%s' % self.tail_number

    class Meta:
        verbose_name_plural = "Tail Numbers"

I am not understanding what is going wrong here.

keepAlive
  • 6,369
  • 5
  • 24
  • 39
Sahana Prabhakar
  • 581
  • 1
  • 8
  • 21
  • 1
    please refer to my answer in your other question. YOu have a badly designed model. And that makes your life 10X complicated. Please fix the model as suggested there – e4c5 May 11 '17 at 04:37

1 Answers1

1

The error you get is probably due to the fact that the dictionary tail_number_data does not contain the keyword aircraft_type, which is expected by TailNumber.objects to create the row in the db, since you defined it with no possibility to be null

aircraft_type = models.ForeignKey(AircraftType, null=False, blank=False)
                                                     ^^^^^

Check that the key "aircraft_type" does exist in the dictionary tail_number_data, or allow for it to be null. Furthermore, if you consider the latter option and that this information is supposed to come from a UI, you may also want to allow for aircraft_type to be blank. See differentiate null=True, blank=True in django for details.

Community
  • 1
  • 1
keepAlive
  • 6,369
  • 5
  • 24
  • 39