Try our conversational search powered by Generative AI!

Field type change

Vote:
 

Hi,
we are trying to change property type from  ContentReference to IList<ContentReference> is there any way to do that in-place or do we need to create a new field and migrate the data?

#276862
Mar 22, 2022 13:54
Vote:
 

I think you'll need to create a new property and migrate the data.

You could have a look at MigrationStep but I'm not sure it can be used here...

#277740
Apr 04, 2022 5:49
Vote:
 

Thomas is right in suggesting the use of MigrationStep, I've used it to create and mutate content items on site start up.  The great part is that they execute once per environment.  You'd have to use the IContent Repository to mutate your old field values onto your new fields and you'd have to iterate over all of your content items with some form of recursive lookup.  I would advise then when you save a content item within a MigrationStep that you use the following save command:

contentRepository.Save(writeablePage, SaveAction.Patch, AccessLevel.NoAccess);

SaveAction.Patch will bypass any validation errors when updating the content.  During a migration step you won't want validation errors to prevent you from converting the field.  The AccessLevel.NoAccess prevents the save event from needing a user with specific privileges to be the person triggering the save event.

An alternative tactic would be to retain both fields and use a method to access them.  If the IList<ContentReference> field is null or empty then fall back to the old ContentReference.  In the interim you could specify that the old version of the field should not be editable by decorating it with:

[ScaffoldColumn(false)]
#277800
Apr 05, 2022 14:02
Vote:
 

I think it is strange because it seems to only be the case for some data types. I changed a property of type int to string and it worked perfectly fine in the cms

#303809
Jun 20, 2023 11:42
Vote:
 

Hi Laurent,

It will be down to the compatability of the fields when swapping out the types.  Int to String isn't really a biggy.  However ContentReference to IList<ContentReference> can be much more complicated.

IList<ContentReference> goes into the [tblContentProperty].[LongString] field as such:

["606f485d-5c54-480c-9b42-695a125f6734","6264447a-71bb-4801-8f03-6442ea067793"]

ContentReference goes into [tblContentProperty].[LinkGuid] as such

14e8403c-321a-4840-bca7-bb161e69dd45

In this scenario a transformation has to take place as the value won't translate, one is essentially a string array while the other is a UID.

#303818
Jun 20, 2023 13:01
Laurent - Jun 20, 2023 13:04
I see! Thank you Mark! Its cool to learn more about how they are persisted in the database
Mark Stott - Jun 20, 2023 13:09
You're welcome :) I had to double check my information and write some queries first :D
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.