U4-10384 - Error when changing content type on media item

Created by Kristian Andersen 05 Sep 2017, 05:56:04 Updated by Sebastiaan Janssen 14 Sep 2017, 11:47:44

Tags: Unscheduled Gold partner

Subtask of: U4-9609

Hi,

I have updated umbraco from 7.3.5 to 7.5.11. Now this piece of code does not work. It was working in umbraco 7.3.5

mediaItem.ChangeContentType(projectImageMediaType, true); mediaService.Save(mediaItem);

When calling .save, umbraco errors: "Cannot insert duplicate key row in object 'dbo.cmsPropertyData' with unique index 'IX_cmsPropertyData_1'. The duplicate key value is (19702, e3767eac-785e-442d-95bc-890696c6df37, 6).\r\nThe statement has been terminated."

6 Attachments

Comments

Sebastiaan Janssen 05 Sep 2017, 06:18:20

Unfortunately I can't reproduce this on a clean install of 7.5.11.

Do you happen to be running this code in an eventhandler? I can see someone had the same error here: http://issues.umbraco.org/issue/U4-9128

The exact code I'm running is:

var mediaItem = ApplicationContext.Current.Services.MediaService.GetById(1082); var projectImageMediaType = ApplicationContext.Current.Services.ContentTypeService.GetMediaType("ImageTest"); mediaItem.ChangeContentType(projectImageMediaType, true); ApplicationContext.Current.Services.MediaService.Save(mediaItem);

When I run this code for the second time, I do get this error. So I need to do a check first, to make sure it is not changing it to the type that it already is:

var mediaItem = ApplicationContext.Current.Services.MediaService.GetById(1082); var projectImageMediaType = ApplicationContext.Current.Services.ContentTypeService.GetMediaType("ImageTest"); if (mediaItem.ContentType.Id != projectImageMediaType.Id) { mediaItem.ChangeContentType(projectImageMediaType, true); ApplicationContext.Current.Services.MediaService.Save(mediaItem); }


Kristian Andersen 05 Sep 2017, 06:41:33

Thats sad. The mediaItem is not the same content type when saving, but some of the properties are the same: Before save mediaItem Id: 19701, Name: "2017-09-04 10_24_55-Gotland Bücherwagen _ Schulz Speyer Bibliothekstechnik AG – Google Chrome.png", ContentType: "Image" ContentType: Id: 1032, Name: "Image", Alias: "Image" ContentTypeBase: Id: 1032, Name: "Image", Alias: "Image" ContentTypeId: 1032 CreateDate: {04/09/2017 11:00:47} CreatorId: 104 HasIdentity: true Id: 19701 Key: {2c638a26-9bd6-4fb7-858a-276f81c4bcba} Level: 4 Name: "2017-09-04 10_24_55-Gotland Bücherwagen _ Schulz Speyer Bibliothekstechnik AG – Google Chrome.png" ParentId: The function evaluation requires all threads to run. Path: "-1,7733,7735,7736,19701" Properties: Count = 6 PropertyGroups: {System.Linq.Enumerable.d__66<Umbraco.Core.Models.PropertyGroup>} PropertyTypes: {System.Linq.Enumerable.d__66<Umbraco.Core.Models.PropertyType>} SortOrder: 19 Trashed: false UpdateDate: {04/09/2017 11:00:47} Version: {9746c73a-699d-4ffc-b647-be771d18d3cc}

after .ChangeContentType - before save: Id: 19701, Name: "2017-09-04 10_24_55-Gotland Bücherwagen _ Schulz Speyer Bibliothekstechnik AG – Google Chrome.png", ContentType: "ProjectImage" ContentType: Id: 7724, Name: "ProjectImage", Alias: "ProjectImage" ContentTypeBase: Id: 7724, Name: "ProjectImage", Alias: "ProjectImage" ContentTypeId: 7724 CreateDate: {04/09/2017 11:00:47} CreatorId: 104 HasIdentity: true Id: 19701 Key: {2c638a26-9bd6-4fb7-858a-276f81c4bcba} Level: 4 Name: "2017-09-04 10_24_55-Gotland Bücherwagen _ Schulz Speyer Bibliothekstechnik AG – Google Chrome.png" ParentId: 7736 Path: "-1,7733,7735,7736,19701" Properties: Count = 17 PropertyGroups: {System.Linq.Enumerable.d__66<Umbraco.Core.Models.PropertyGroup>} PropertyTypes: {System.Linq.Enumerable.d__66<Umbraco.Core.Models.PropertyType>} SortOrder: 19 Trashed: false UpdateDate: {04/09/2017 11:00:47} Version: {9746c73a-699d-4ffc-b647-be771d18d3cc} An Exception is thrown: "Cannot insert duplicate key row in object 'dbo.cmsPropertyData' with unique index 'IX_cmsPropertyData_1'. The duplicate key value is (19701, 9746c73a-699d-4ffc-b647-be771d18d3cc, 6).\r\nThe statement has been terminated." The code being executed is not in an eventhandler Full code below: [HttpGet] public ActionResult UpdateMediaItemQueueItems() { try { var itemsToUpdate = Lammhults.Helpers.DatabaseHelper.GetMediaItemQueueItems(Lammhults.Helpers.MediaItemQueueStatus.Pending).Select(x => x.MediaItemId); var mediaService = Services.MediaService; var mediaItems = mediaService.GetByIds(itemsToUpdate); IMediaType projectImageMediaType = Services.ContentTypeService.GetMediaType("ProjectImage");

            foreach (var mediaItem in mediaItems)
            {
                try
                {
                    //Lammhults.Helpers.DatabaseHelper.UpdateMediaItemQueueStatus(mediaItem.Id, Lammhults.Helpers.MediaItemQueueStatus.Working);
                    mediaItem.ChangeContentType(projectImageMediaType, true);
                    mediaService.Save(mediaItem);
                    //Lammhults.Helpers.DatabaseHelper.UpdateMediaItemQueueStatus(mediaItem.Id, Lammhults.Helpers.MediaItemQueueStatus.Success);
                }
                catch (Exception e)
                {

                    //Shared.Infrastructure.ApplicationContext.Logger.Error("Error updating mediaqueueitem Error: " + e.Message, e);
                    //Lammhults.Helpers.DatabaseHelper.UpdateMediaItemQueueStatus(mediaItem.Id, Lammhults.Helpers.MediaItemQueueStatus.Failed);
                }
            }
        }
        catch (Exception e)
        {
            Shared.Infrastructure.ApplicationContext.Logger.Error(
                "UpdateMediaItemQueueItems " + DateTime.Now.ToLongTimeString() + "Error: " + e.Message, e);

        }
        return new HttpStatusCodeResult(HttpStatusCode.OK);
    }

It seems like dublicate items are being created in the cmspropertydata table, causing a conflict on the index. Attached file displayes the values stored in the table for the node where the content type is changed.


Kristian Andersen 05 Sep 2017, 06:45:16

@sebastiaan have you added additional properties to the ImageTest media type?


Kristian Andersen 05 Sep 2017, 07:14:06

@sebastiaan lookes like it errors when changing content type to a content type that uses the current content type as a composition... See attached file. When changing to test2 mediatype, the same error occurs in a clean 7.5.11 environment


Kristian Andersen 06 Sep 2017, 07:09:45

@sebastiaan did you manage to reproduce the issue?


Kristian Andersen 07 Sep 2017, 08:15:22

Steps to reproduce on clean umbraco 7.5.11:

  1. Create media type "ImageTest", Where the image type inherits the "Image" media type. (Screenshot attached)
  2. Add an image to the media library.
  3. Change the content type from Image to ImageTest var mediaItem = ApplicationContext.Current.Services.MediaService.GetById(1062); var projectImageMediaType = ApplicationContext.Current.Services.ContentTypeService.GetMediaType("ImageTest"); mediaItem.ChangeContentType(projectImageMediaType, true); ApplicationContext.Current.Services.MediaService.Save(mediaItem);
  4. See the error (screenshot attached)


Claus Jensen 07 Sep 2017, 11:14:06

PR: https://github.com/umbraco/Umbraco-CMS/pull/2177

Ensuring ID is set on the property along with the value, to avoid trying to insert a new property (having ID: 0)


Kristian Andersen 07 Sep 2017, 11:37:46

@claus I have tested the fix, and even though it changes the content type correctly, it seems like the existing property values are not kept when setting the clearProperties parameter to false.

In this case, the content type of the image is changed, but the image data is lost.

mediaItem.ChangeContentType(projectImageMediaType, false);


Claus Jensen 07 Sep 2017, 12:43:52

@kristian@struct.dk when I'm testing this, the property values are never cleared. It will retain the data in the properties (all of the meta data and such) and the image works just as it did before the type change.

What version of core are you testing this on?

Also - I don't believe the clearProperties flag is supposed to do what you think it does. The flag will toggle whether we clear out the existing ''collection'' of properties to combine in a composition before adding the properties to the composition we're working with. It is not used to clear actual value data of the properties. I can see that the description of the method on the service is a bit vague in regards to what it actually does...


Kristian Andersen 08 Sep 2017, 06:32:28

@claus im testing on umbraco 7.5.11 - the values in cmsPropertyData are nulled when changing the content type. Thank you for the update on clearProperties - you are right.


Kristian Andersen 08 Sep 2017, 06:38:59

cmsPropertyData before and after contenttypechange attached


Claus Jensen 08 Sep 2017, 10:07:14

@kristian@struct.dk Yes .. I think the clearing of that property data that you're seeing, may be happening due to those properties actually not being maintained with the same link anymore due to not having the Id set (which is what I fixed). So I guess being able to clear the data, could be seen as a unintentional "feature" that was never really meant to be.

I think the reason why this all has worked before and seems to be failing in that later version, may be due to us having fixed some checks in the data layer so it actually does some validation on the data it is trying to insert. Unfortunately there's a lot of places where things have been working just by chance, which we find out when we actually implement checks and validation for the data we're handling.


Kristian Andersen 11 Sep 2017, 12:27:22

@claus This is still a major issue to our client, as they depend on being able to change the content type. I do belive that when changing a content type from an image to a different content type (inheriting from the image contenttype), the basic propertydata of the media item should be maintained.

Do you think this will be fixed?


Claus Jensen 11 Sep 2017, 13:33:40

@kristian@struct.dk I'm not sure I understand what's wrong here. The image data is not cleared when changing the content type here on my tests with 7.6.6... and that is regardless of what I set the second parameter to (since this parameter really doesn't have anything to do with this).


Kristian Andersen 11 Sep 2017, 13:53:08

@claus the imagedata is cleared in version 7.5.11.

I cannot upgrade umbraco at the moment so im forced to discard my mediatype and create a new mediatype not using compositions and switch to using that instead.


Stephan 11 Sep 2017, 14:58:01

Right, just tested this:

  • get 7.5.12 sources
  • apply @claus PR fix
  • create an Image media
  • create an ImageTest media type, composed of Image
  • ran the following code

var mediaItem = ApplicationContext.Current.Services.MediaService.GetById(3398); var projectImageMediaType = ApplicationContext.Current.Services.ContentTypeService.GetMediaType("ImageTest"); mediaItem.ChangeContentType(projectImageMediaType, true); ApplicationContext.Current.Services.MediaService.Save(mediaItem);

And:

  • no exception (thanks to the PR fix)
  • media still shows the image in the back-office
  • table cmsPropertyData property values have been preserved

@claus @kristian@struct.dk can we verify that the "lost values" issue is indeed fixed with 7.5.12 (in which case appying the PR fix to 7.5.12 would be a rather simple task, and upgrading to 7.5.12 from 7.5.11 equally simple)?


Claus Jensen 12 Sep 2017, 07:15:55

@zpqrtbnk @kristian@struct.dk actually I've just tested this with a completely clean 7.5.11 site (and only my fix for the ID property applied).

Changing the media type from Image to ImageTest, using the exact code @kristian@struct.dk wrote above .. I do not lose any data and my image still shows - and the media type has changed from Image to ImageTest.

I believe it must be something else causing the issue you're seeing @kristian@struct.dk .. are you completely sure you are testing this in a clean site with no other custom code?


Claus Jensen 12 Sep 2017, 07:22:20

Here's a screen recording of me not being able to replicate the issue ... http://recordit.co/yTTTnDXr0b


Sebastiaan Janssen 14 Sep 2017, 11:47:39

I've merged this for 7.6.7 while we're waiting for feedback on the disappearing properties. We'll consider this Fixed and we can apply this fix to 7.5.11 at any time later.


Priority: Normal

Type: Bug

State: Fixed

Assignee:

Difficulty: Normal

Category:

Backwards Compatible: False

Fix Submitted:

Affected versions: 7.5.11

Due in version: 7.6.7

Sprint: Sprint 67

Story Points: 1

Cycle: 4