snipe-it icon indicating copy to clipboard operation
snipe-it copied to clipboard

Cannot delete location after removing assigned assets

Open doc-jay opened this issue 10 years ago • 6 comments

Version v3.6.0-pre-41-ga914dac

Expected Behavior (or desired behavior if a feature request)

expected to be able to delete a location after reassigning assets to different location.


Actual Behavior

When the location is viewed, it lists a count of assets still assigned. When the location hyperlink is clicked, there are no assets to be viewed.


Please confirm you have done the following before posting your bug report:

  • [ ] I have enabled debug mode - NO

Please provide answers to these questions before posting your bug report:

  • Version of Snipe-IT you're running - v3.6.0-pre-41-ga914dac
  • What OS and web server you're running Snipe-IT on - CENTOS 7; NGINX
  • What method you used to install Snipe-IT (install.sh, manual installation, docker, etc) Manual install
  • If you're getting an error in your browser, include that error - N/A
  • What specific Snipe-IT page you're on, and what specific element you're interacting with to trigger the error - locations page, error when deleting location - but I have reassigned all assets to different location.
  • If a stacktrace is provided in the error, include that too. N/A
  • Any errors that appear in your browser's error console. NONE
  • Confirm whether the error is reproduceable on the demo. - the demo will not allow me to delete locations, - not allowed by the demo user.
  • Include any additional information you can find in app/storage/logs and your webserver's logs. - nothing useful there
  • Include what you've done so far in the installation, and if you got any error messages along the way. - the only error I get is when I try to delete the 'old' location.
  • Indicate whether or not you've manually edited any data directly in the database - N/A

doc-jay avatar Nov 12 '16 17:11 doc-jay

Do you have any users or anything else still assigned to that location?

snipe avatar Nov 13 '16 22:11 snipe

Nope, I've set the location for the user that used to be assigned to that location to 'select a location' already.

doc-jay avatar Nov 13 '16 22:11 doc-jay

Hmm...

I think this has to do with the following relationship:

class Location {
/* trimmed */
    public function assets()
    {
        return $this->hasManyThrough('\App\Models\Asset', '\App\Models\Actionlog', 'location_id', 'id');
    }
/* trimmed */
}

I don't think logs would ever go away, so if an asset has ever been checked out to a user assigned to given location, it would look like at present that location cannot be deleted? Pondering now if there's a more direct way to write that query that does not involving going through the logs table.

mnemonicly avatar Nov 14 '16 05:11 mnemonicly

Can you try the following patch? It seems to behave locally, but I don't have a perfect environment at the moment to try to reproduce:

diff --git a/app/Models/Location.php b/app/Models/Location.php
index 2f7dc9d..4bf4eae 100755
--- a/app/Models/Location.php
+++ b/app/Models/Location.php
@@ -49,7 +49,8 @@ class Location extends Model

     public function assets()
     {
-        return $this->hasManyThrough('\App\Models\Asset', '\App\Models\Actionlog', 'location_id', 'id');
+
+        return $this->hasManyThrough('\App\Models\Asset', '\App\Models\User', 'location_id', 'assigned_to', 'id');
     }

     public function assignedassets()

Essentially just adjust the one line in app/Models/Location.php (around line 53 depending on your version) so that the hasManyThrough line reads return $this->hasManyThrough('\App\Models\Asset', '\App\Models\User', 'location_id', 'assigned_to', 'id');

I'll try to give this more testing tomorrow, along with any feedback you can provide.

mnemonicly avatar Nov 14 '16 05:11 mnemonicly

I gave this a try - still got the error: ' Error: This location is currently associated with at least one asset and cannot be deleted. Please update your assets to no longer reference this location and try again. ' When I click on the hyperlink for the location, it shows 'No matching records found' for the user & asset tag sections.

EDIT: this DID work - didn't save my change correctly. I noticed that and tried it again and I was able to delete the location entry I needed. Thx!

doc-jay avatar Nov 15 '16 01:11 doc-jay

I was also experiencing this issue.

Changing that line in Location.php resolved the issue for me aswell.

delinski111 avatar Nov 22 '16 18:11 delinski111