cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Editing a unique index on Customer

Former Member
0 Likes
2,024

The itemtype Customer has a unique index UID through the super itemtype Principal. Principal also lists uid as a unique attribute.

I need to change this index to add an additional column to the unique index.

I tried the following, and it does not work.

 <itemtype code="Customer" autocreate="false" generate="false">
     <attributes>
         <attribute type="Banner" qualifier="banner">
             <persistence type="property" />
         </attribute>
     </attributes>
     <indexes>
         <index name="UID" unique="true" replace="true">
             <key attribute="uid"/>
             <key attribute="banner"/>
         </index>
     </indexes>
 </itemtype>

I tried this as well.

 <itemtype code="Customer" autocreate="false" generate="false">
     <attributes>
         <attribute qualifier="uid" type="java.lang.String" redeclare="true">
             <persistence type="property"/>
             <modifiers read="true" write="true" search="true" optional="false" unique="false"/>
             <custom-properties>
                 <property name="hmcIndexField">
                     <value>"thefield"</value>
                 </property>
             </custom-properties>
         </attribute>
         <attribute type="Banner" qualifier="banner">
             <persistence type="property" />
         </attribute>
     </attributes>
     <indexes>
         <index name="UID" unique="true" replace="true">
             <key attribute="uid"/>
             <key attribute="banner"/>
         </index>
     </indexes>
 </itemtype>
 

When I run the updatesystem, there is no change.

  [java] INFO  [main] [HybrisSchemaGenerator] Read type system Information. Time taken 1.773 s
  [java] INFO  [main] [HybrisSchemaGenerator] Preparing  Db model.
  [java] INFO  [main] [DatabaseModelGenerator] Index (UID_4) already exists  for type (4)  with table (users)
  [java] INFO  [main] [DatabaseModelGenerator] Prepared Database Model with (876) tables.
  [java] INFO  [main] [HybrisSchemaGenerator] Prepared  Db model. Time taken 7.896 s
  [java] INFO  [main] [HybrisSchemaGenerator] Preparing target and source models
 


Can anyone point me to the right way of doing it. I am using Hybris 6.4 by the way.

Accepted Solutions (0)

Answers (4)

Answers (4)

Former Member
0 Likes

I am also trying to override unique index of an item type. Which solution worked the best for you?

Former Member
0 Likes

I was not able to. UID was a unique key on the Principal and I cannot remove that. Even if I managed to remove the unique key for the Customer table, I ran into a bunch of validators that were not letting me insert data which conflicts with the unique key. So, I abandoned the attempt.

former_member910831
Discoverer
0 Likes

You need to redeclare the Principal UID index with additional modifiers and then add the new index that overrides the old one (maybe it is additionally needed to delete the old index manually):

 <itemtype code="Customer" autocreate="false" generate="false">
     <attributes>
         <attribute qualifier="cmsSite" type="CMSSite">
             <modifiers read="true" write="true" search="true" optional="false" unique="true"/>
             <persistence type="property"/>
         </attribute>
     </attributes>
     <indexes>
         <index name="UID" unique="true">
             <key attribute="uid"/>
             <key attribute="cmsSite"/>
         </index>
     </indexes>
 </itemtype>
 <itemtype code="Principal" autocreate="false" generate="false">
     <indexes>
         <index name="UID" remove="true" replace="true">
             <key attribute="uid"/>
         </index>
     </indexes>
 </itemtype>

Former Member
0 Likes

As you have noticed the default UniqueAttributesInterceptor will prevent you even before index constraint will fail the operation, what you should do:

  1. don't redeclate UID attribute - there is no point in doing this

  2. declare new banner attribute as unique also in order to bypass UniqueAttributesInterceptor

  3. declare new unique composite index with 2 attributes

  4. create an SQL script which will drop default UID only index directly from DB, you can execute it in a class which listens for init/update messages

andyfletcher
Active Contributor
0 Likes

A system update won't change existing indexes.

I think if you add it as a new index (i.e. give it a new name) then it should be created on system update.

Otherwise your other option is system initialise (which presumably isn't viable if you want to keep your data) or manually altering the index in the database yourself.

Former Member
0 Likes

Thanks Andrew. Is it possible to delete the existing index - the index UID that I mentioned in the original post. I can of course delete it in the database. But, the next update system recreates the index.

My plan is to drop the index and add a duplicate row with the same uid, but satisfying the new composite unique index that I create.

So, when the update system tries to create the original unique index, it will fail because of the duplicate.

I know this is a very dirty trick 🙂 But if you have any other suggestion to remove an index, please let me know.

Former Member
0 Likes

ok. My plan fell flat. UniqueAttributesInterceptor stops me from creating another record with the same uid even if I drop the unique index from the database

Former Member
0 Likes

Why can you not rename your index (maybe uid_banner)?

Cheers, Thomas