Uploaded image for project: 'Hybrid Cloud Console'
  1. Hybrid Cloud Console
  2. RHCLOUD-20441

Remove the structs with ids that are then used as "where" conditions

XMLWordPrintable

    • Icon: Story Story
    • Resolution: Done
    • Icon: Undefined Undefined
    • None
    • None
    • None
    • 3
    • False
    • Hide

      None

      Show
      None
    • False
    • None
    • Unset
    • None
    • Sources Sprint 45, Sources Sprint 46, Sources Sprint 47, Sources Sprint 48

      The following code structure to get things by its ID:

      	app := &m.Application{ID: *id}
      	result := DB.Debug().
      		Where("tenant_id = ?", a.TenantID).
      		First(&app)
      	if result.Error != nil {
      		return nil, util.NewErrNotFound("application")
      	}
      

      Tends to be problematic when the struct has a default value, since Gorm tends to ignore those and removes the "Where" condition for those. So in the above example, if we provided an ID with the value 0, Gorm simply issues the following query:

      SELECT
          *
      FROM
          applications
      WHERE
          tenant_id = ?
      LIMIT 1
      

      Which results in unexpected behaviour.

              mbarcina@redhat.com Mikel Barcina
              mbarcina@redhat.com Mikel Barcina
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved: