#update_or_create() issue

4 messages · Page 1 of 1 (latest)

steep jetty
#

Folks, I've never used update_or_create() before, and I'm getting a weird problem. The select ... for update doesn't look right to me, and it's not detecting that this is an update instead of a create. The query I'm getting is:

SELECT `foodinspection_dev`.`header_temp`.`inspectionID`,
       `foodinspection_dev`.`header_temp`.`prevInspectionID`,
       `foodinspection_dev`.`header_temp`.`foodServiceID`,
       ... (All the fields in the table)
  FROM `foodinspection_dev`.`header_temp`
 WHERE (`foodinspection_dev`.`header_temp`.`date` IS NULL AND `foodinspection_dev`.`header_temp`.`email` = '[email protected]' AND `foodinspection_dev`.`header_temp`.`foodServiceID` = 762 AND `foodinspection_dev`.`header_temp`.`inspectionID` = 4159 AND `foodinspection_dev`.`header_temp`.`inspectorID` = 58 AND `foodinspection_dev`.`header_temp`.`manager` = 'Kyle New' AND `foodinspection_dev`.`header_temp`.`obsB` = 1 AND `foodinspection_dev`.`header_temp`.`obsC` = 0 AND `foodinspection_dev`.`header_temp`.`obsD` = 0 AND `foodinspection_dev`.`header_temp`.`obsL` = 0 AND `foodinspection_dev`.`header_temp`.`obsO` = 0 AND `foodinspection_dev`.`header_temp`.`prevInspectionID` IS NULL AND `foodinspection_dev`.`header_temp`.`serB` = 1 AND `foodinspection_dev`.`header_temp`.`serC` = 0 AND `foodinspection_dev`.`header_temp`.`serD` = 0 AND `foodinspection_dev`.`header_temp`.`serL` = 1 AND `foodinspection_dev`.`header_temp`.`serO` = 0 AND `foodinspection_dev`.`header_temp`.`status` = 1 AND `foodinspection_dev`.`header_temp`.`timeIn` = '13:25:00.000000' AND `foodinspection_dev`.`header_temp`.`timeOut` = '13:25:00.000000' AND `foodinspection_dev`.`header_temp`.`inspectionType` = 2)
 LIMIT 21
   FOR UPDATE```

I would expect the `where` of that query to just be ``foodinspection_dev`.`header_temp`.`inspectionID` = 4159`, since that's the pkey
#

My model looks like:

class IInspectionHeaderTemp(models.Model):
    """Holds the header information for inspections in a temporary state (to be later pulled into inspections_header table in the PHP system)"""

    inspection_id = models.IntegerField(primary_key=True, db_column="inspectionID")
    previous_inspection = models.ForeignKey(
        "self", db_column="prevInspectionID", on_delete=models.DO_NOTHING, null=True
    )
    food_service = models.ForeignKey(IFoodService, db_column="foodServiceID", on_delete=models.DO_NOTHING, null=True)
    inspector = models.ForeignKey(IInspector, db_column="inspectorID", on_delete=models.DO_NOTHING, null=True)
    type = models.ForeignKey(IInspectionType, db_column="inspectionType", on_delete=models.DO_NOTHING, null=True)
    status = models.IntegerField(null=True)
    date = models.DateField(null=True)
    time_in = models.TimeField(db_column="timeIn", null=True)
    time_out = models.TimeField(db_column="timeOut", null=True)
    manager = models.CharField(max_length=50, null=True)
    email = models.CharField(max_length=100, null=True)
    serves_breakfast = models.BooleanField(db_column="serB", null=True)
    serves_lunch = models.BooleanField(db_column="serL", null=True)
    serves_dinner = models.BooleanField(db_column="serD", null=True)
    serves_catering = models.BooleanField(db_column="serC", null=True)
    serves_other = models.BooleanField(db_column="serO", null=True)
    observed_breakfast = models.BooleanField(db_column="obsB", null=True)
    observed_lunch = models.BooleanField(db_column="obsL", null=True)
    observed_dinner = models.BooleanField(db_column="obsD", null=True)
    observed_catering = models.BooleanField(db_column="obsC", null=True)
    observed_other = models.BooleanField(db_column="obsO", null=True)

    class Meta:
        managed = False
        db_table = f"{settings.FOOD_INSPECTION_SCHEMA}`.`header_temp"```
#

Am I using this wrong, or is there an error in my model, or is something else going on?

#

My method that is calling the update_or_create is:

    def push_to_inspection_header_temp(self):
        """Insert this inspection into the InspectionHeaderTemp model for the PHP system"""

        log.debug("Pushing to InspectionHeaderTemp")

        inspection_models.IInspectionHeaderTemp.objects.update_or_create(
            inspection_id=self.inspection_id,
            previous_inspection=self.previous_inspection,
            food_service=self.food_service,
            inspector=self.inspector,
            type=self.type,
            ... (A bunch of other fields)