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

Importer creating duplicate models

Open joepitt91 opened this issue 8 years ago • 10 comments

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

Describe the bug Every time I run an import snipe-it is creating a duplicate asset model and assigning imported items in that import to it rather than the existing model of the identical name, manufacturer & category.

To Reproduce Steps to reproduce the behavior:

  1. Create a CSV, with fields Item Name,Category,Location,Model Name,Manufacturer,Asset Tag,Status
  2. Populate the CSV with assets, directly copying the Model Name, Manufacturer & Category (all mandatory fields - https://snipe-it.readme.io/docs/importing) from an existing Model.
  3. Go to /import
  4. Upload the CSV
  5. Process the CSV as assets, ensuring all fields match correctly
  6. Go to /models
  7. See duplicated model

Expected behavior As per documentation the importer should check if the same model already exists, if so it should like imported items to this rather than create a new, duplicated, model.

Screenshots If applicable, add screenshots to help explain your problem.

Server (please complete the following information):

  • Snipe-IT Version v4.6.5
  • OS: [e.g. Ubuntu, CentOS] Ubuntu
  • Web Server: [e.g. Apache, IIS] Apache
  • PHP Version 7.2.10

Desktop (please complete the following information):

  • OS: [e.g. iOS] Windows 10
  • Browser [e.g. chrome, safari] Chrome
  • Version [e.g. 22] 69

Error Messages

  • WITH DEBUG TURNED ON, if you're getting an error in your browser, include that error No errors reported
  • If a stacktrace is provided in the error, include that too. No errors reported
  • Any errors that appear in your browser's error console. No errors reported
  • Confirm whether the error is reproducible on the demo: https://snipeitapp.com/demo. No - Importing disabled on Demo
  • Include any additional information you can find in storage/logs and your webserver's logs. Nothing further to add

Additional context

  • Is this a fresh install or an upgrade? fresh
  • What OS and web server you're running Snipe-IT on Ubuntu & Apache
  • What method you used to install Snipe-IT (install.sh, manual installation, docker, etc) recommended git approach
  • Include what you've done so far in the installation, and if you got any error messages along the way. basic configuration
  • Indicate whether or not you've manually edited any data directly in the database No

Add any other context about the problem here.

Please do not post an issue without answering the related questions above. If you have opened a different issue and already answered these questions, answer them again, once for every ticket. It will be next to impossible for us to help you.

joepitt91 avatar Oct 07 '18 18:10 joepitt91

This is a bug, I've seen it a few times

EarlRamirez avatar Oct 08 '18 21:10 EarlRamirez

I haven't been able to reproduce this. Can someone provide me with a sample CSV that triggers the issue?

snipe avatar Oct 19 '18 09:10 snipe

@snipe this was pointed out by @dmeltzer on #5805, I will try and reproduce it in my test environment and share the sample CSV.

EarlRamirez avatar Oct 19 '18 09:10 EarlRamirez

@snipe I was able to reproduce it, if the initial CSV header contains Model Name and the consecutive CSV contains a Model Name and a Model Number a new asset model will be created.

EarlRamirez avatar Oct 19 '18 09:10 EarlRamirez

I believe that I might find the cause of this in the ItemImporter.php.

{

    $asset_model_name = $this->findCsvMatch($row, "asset_model");
    $asset_modelNumber = $this->findCsvMatch($row, "model_number");
    // TODO: At the moment, this means  we can't update the model number if the model name stays the same.
    if (!$this->shouldUpdateField($asset_model_name)) {
        return;
    }
    if ((empty($asset_model_name))  && (!empty($asset_modelNumber))) {
        $asset_model_name = $asset_modelNumber;
    } elseif ((empty($asset_model_name))  && (empty($asset_modelNumber))) {
        $asset_model_name ='Unknown';
    }
    $editingModel = $this->updating;
    $asset_model = AssetModel::where(['name' => $asset_model_name, 'model_number' => $asset_modelNumber])->first();

    if ($asset_model) {
        if (!$this->updating) {
            $this->log("A matching model already exists, returning it.");
            return $asset_model->id;
        }
        $this->log("Matching Model found, updating it.");
        $item = $this->sanitizeItemForStoring($asset_model, $editingModel);
        $item['name'] = $asset_model_name;
        $item['model_number'] = $asset_modelNumber;
        $asset_model->update($item);
        $asset_model->save();
        $this->log("Asset Model Updated");
        return $asset_model->id;
    }
    $this->log("No Matching Model, Creating a new one");

    $asset_model = new AssetModel();
    $item = $this->sanitizeItemForStoring($asset_model, $editingModel);
    $item['name'] = $asset_model_name;
    $item['model_number'] = $asset_modelNumber;

    $asset_model->fill($item);
    $item = null;

    if ($asset_model->save()) {
        $this->log('Asset Model ' . $asset_model_name . ' with model number ' . $asset_modelNumber . ' was created');
        return $asset_model->id;
    }
    $this->logError($asset_model, 'Asset Model "' . $asset_model_name . '"');
    return null;
}

EarlRamirez avatar Oct 19 '18 10:10 EarlRamirez

if the initial CSV header contains Model Name and the consecutive CSV contains a Model Name and a Model Number

I'm not sure what you mean by consecutive CSV. Can you explain?

If you have a model named Foo, and in one row of the CSV you have Model Name Foo listed without a model number, and in the next row, you have model name Foo with a model number, the system will see those as two different models. It checks for a name+model number pair. That's expected behavior though, as there are many circumstances where a model name could have many different possible model numbers.

snipe avatar Oct 19 '18 11:10 snipe

if the initial CSV header contains Model Name and the consecutive CSV contains a Model Name and a Model Number

I'm not sure what you mean by consecutive CSV. Can you explain?

Say you import an few assets with only a model name (all assets in the CSV has the same model number) and you add a field to the existing CSV, e.g. IP address, but this time you add the model number but the model number is not populated, a new model will be created and all existing assets will be moved to this "new" model.

If you have a model named Foo, and in one row of the CSV you have Model Name Foo listed without a model number, and in the next row, you have model name Foo with a model number, the system will see those as two different models. It checks for a name+model number pair. That's expected behavior though, as there are many circumstances where a model name could have many different possible model numbers.

Now I understand, since it is looking for a pair then this explains why the duplicate models are being created, does the pair also look for case sensitivity as well?

EarlRamirez avatar Oct 19 '18 11:10 EarlRamirez

Is this still relevant? We haven't heard from anyone in a bit. If so, please comment with any updates or additional detail. This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Don't take it personally, we just need to keep a handle on things. Thank you for your contributions!

stale[bot] avatar Dec 19 '18 04:12 stale[bot]

This issue has been automatically closed because it has not had recent activity. If you believe this is still an issue, please confirm that this issue is still happening in the most recent version of Snipe-IT and reply to this thread to re-open it.

stale[bot] avatar Dec 27 '18 01:12 stale[bot]

I have 2 issues regarding model name.

  1. When I import assets with Model Name, the picture is imported with it however when I go to Settings\Asset Model the picture is not there but in Asset view picture comes up. Now, if I edit asset model to add picture from settings the next issue comes up...
  2. Next import of the same model will create another Asset Model.

How can this be fixed, or what should be the correct way to import assets so it can have pictures in the Asset view and Asset Model.

patelcsobeys avatar Jul 26 '20 17:07 patelcsobeys