<-- Back

Deployment fails with “ERROR: could not create unique index”

Issue

When deploying a new version of an application in Mendix Cloud, the deployment may fail during the Execute Data Definition Language (DDL) commands step. This step takes place during the application startup if there are domain model changes - such as new entities, attributes, or index definitions. During this step, Mendix tries to align the database structure with the updated domain model by running DDL commands. These commands create, alter, or drop database tables, columns, indexes, and constraints as needed.

If duplicated data exists in the database that violates a new or updated unique index, the following error (or a similar one) may appear in the logs:

ERROR: could not create unique index "uniq_modulename$entity_attribute"
Detail: Key ("attribute")=(value) is duplicated.

This error prevents the application from starting after deployment.

Environment

Applications hosted in any deployment type.

Cause

This issue occurs when the deployment package includes a domain model where an attribute is marked as unique. Mendix automatically attempts to create a corresponding unique index in the database for that attribute during deployment.

This step is part of the Execute DDL commands phase, where Mendix aligns the database with the domain model. If the target database contains duplicate values for the attribute, the database cannot create the required unique index. As a result, the DDL execution fails, and the application cannot start.

This can happen even if the same package successfully deploys to another environment, as data can vary across environments. 

Solution / Workaround

This issue requires manual intervention, as it is related to data integrity in the database. Since the deployment cannot proceed with the current data violating unique constraints, the following approach is recommended.

To bring the application back online, in case it is a production environment, restore the previous working version of the app along with a backup taken before the deployment. Then, use a local or non-production environment with the latest production backup to identify and clean up the duplicate records of the entity's attribute for which an index is attempted to be created.

Follow these steps:

  1. Restore the backup locally or in a non-production environment.
  2. Investigate the entity mentioned in the error log.
  3. For example, if the error refers to ERROR: could not create unique index "uniq_modulename$entity_attribute" , check the relevant entity and attribute in the database for duplicate values.
  4. Remove or merge the duplicate records from the affected table or association.

  5. Review the unique constraint in the domain model. Confirm whether this constraint is still appropriate for the current dataset, and consider whether any changes are needed, such as removing the constraint or applying it to a different attribute.

  6. Restore the backup that was cleaned in step 4.

  7. Deploy the updated package only after ensuring that no duplicates remain in the database and that the domain model changes can be applied without errors.

If modifying production data directly is not feasible, consult with the development or database team to safely handle this process.

Internal information related

  • 255489, 251355, 179405, 251713, 252805
  • C5SASDZBN/p1678101850950779

Additional information

Mendix documentation: 

Have more questions? Submit a request

0 Comments

Article is closed for comments.

To provide feedback, please open a ticket here. Don't forget to include the article's URL along with the feedback you would like to provide.